vector resize doesn't work? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have 3 maps with a string as key and a vector as value.
std::map<std::string, std::vector<int> > m_attri; ///< Attributes of type int
std::map<std::string, std::vector<double> > m_attrd; ///< Attributes of type double
std::map<std::string, std::vector<std::string> > m_attrs; ///< Attributes of type std::string
I have a function that resizes them all, calles SetNum()
void SetNum(size_t n)
{
std::cout << "setting num: " << n << std::endl;
m_num = n;
for (auto attr : m_attri) {
attr.second.resize(n, 0);
std::cout<<attr.second.size();
}
for (auto attr : m_attrd) {
attr.second.resize(n, 0.0);
std::cout<<attr.second.size();
}
for (auto attr : m_attrs) {
attr.second.resize(n, "");
std::cout<<attr.second.size();
}
std::cout<<std::endl;
}
this all seems to work, when is say setnum(1), I get a bunch of 1s as output.
Now the problem lies here, I have another function called Set
template<typename T>
inline void AttributeContainer::Set(size_t node_idx, const std::string& name, T value)
{
std::cout << GetNum() << node_idx << ',' << std::endl;
if( node_idx >= GetNum()){
SetNum(node_idx+1);
}
auto attr_kv = Attributes<T>().find(name);
if (attr_kv != Attributes<T>().end()) {
std::cout<<"Containersize: " << attr_kv->second.size() << ',' << "id:"<< node_idx<< std::endl;
attr_kv->second.at(node_idx) = value;
std::cout<<"ContainersizeDone: " << attr_kv->second.size() << std::endl;
} else {
throw std::runtime_error("AttributeContainer::Set(): attribute not found: " + name);
}
}
node_idx is 0 and the vector is found. But it seems that the resize didn't work, because, altough I get output that confirms it is resized, i get a out_of_range exception from at and the line above shows that the size is 0. Now this is weird, as I thought that I succesfully resized them?

In SetNum, you are iterating over your map by value (see this post)
Consequently, you are applying resize on copies of your stored vectors.
What you want is to use auto& :
void SetNum(size_t n)
{
std::cout << "setting num: " << n << std::endl;
m_num = n;
for (auto& attr : m_attri) {
attr.second.resize(n, 0);
std::cout<<attr.second.size();
}
for (auto& attr : m_attrd) {
attr.second.resize(n, 0.0);
std::cout<<attr.second.size();
}
for (auto& attr : m_attrs) {
attr.second.resize(n, "");
std::cout<<attr.second.size();
}
std::cout<<std::endl;
}

Related

How can you print integers from a class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
This seems like a simple thing to me, but I have an entity class:
class Entity
{
private:
std::string m_Name;
public:
int x = 0, y = 0;
Entity() : m_Name("Undefined Entity") {
std::cout << m_Name << " Created!\n";
}
Entity(const std::string& name) : m_Name(name) {
std::cout << m_Name << " Created!\n";
}
~Entity() {
std::cout << m_Name << " Destroid\n";
}
const std::string GetName() { return m_Name; }
void PrintCords() { std::cout << x << ' , ' << y << '\n'; }
};
and in my main function I run the PrintCords function:
int main(void)
{
std::unique_ptr<Entity> j = std::make_unique<Entity>("Jeff");
std::cout << j->GetName() << '\n';
j->PrintCords();
}
Yet, when I run the program, I get seemingly random numbers:
021084480
You should compile with -Werror. This turns your warnings into errors. When you do this, you will see the following error:
prog.cpp:25:43: warning: multi-character character constant [-Wmultichar]
void PrintCords() { std::cout << x << ' , ' << y << '\n'; }
^~~~~
Which should indicate to you that something is wrong. If you change this to a string literal:
void PrintCords() { std::cout << x << " , " << y << '\n'; }
---> ^ ^
Then everything will be ok.

What exactly does this compiler error want me to perform? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Thank you in advance for reading ! So this is the code :
#include<iostream>
#include<vector>
std::vector<int> MonkeyCount(int n);
int main() {
MonkeyCount(4);
return 0;
}
std::vector<int> MonkeyCount(int n) {
std::vector<int> MonkeyCountV;
for (unsigned int i = 1; i <= n; i++) {
MonkeyCountV.push_back(i);
}
for (unsigned int i = 0; i <= MonkeyCountV.size(); i++) {
std::cout << MonkeyCount.at(i) << " ";
}
return MonkeyCountV;
}
and the error is on line 23 : error C2227: left of '->at' must point to class/struct/union/generic type
Now i red something about this, but i use this from an example i found on the internet on how to print a vector, and in that exaple, in works. The exaple is this :
#include <iostream>
#include <vector>
void print(std::vector<int> const& input);
int main()
{
std::vector<int> input = { 1, 2, 3, 4, 5 };
print(input);
return 0;
}
void print(std::vector<int> const& input)
{
for (unsigned int i = 0; i < input.size(); i++) {
std::cout << input.at(i) << ' ';
}
}
std::cout << MonkeyCount.at(i) << " ";
Should be:
std::cout << MonkeyCountV.at(i) << " ";
The way you have it is trying to do ".at(i)" on the function itself.
The name of your vector is MonkeyCountV but in the cout statement you are using MonkeyCount which is actually the name of your function. Make sure you have your variables name typed correctly.

Mapping an action name to a class type in C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I want to link a bunch of 'action' strings to single 'parent' string but there could be multiple strings that own 'action' strings.
map<string, string> ctType;
ctType.insert(pair<string, string>("1")("default"));
ctType.insert(pair<string, string>("2")("register"));
ctType.insert(pair<string, string>("2")("addaddress"));
ctType.insert(pair<string, string>("3")("request"));
What is the best way to complete this?
You could either (1) use std::multimap, or you could (2) use a map with containers as its elements. Variant (1) is rather short, but has the drawback that it is harder to control how the "multiple entries" behave in terms of, for example, duplicates; and its probably harder to implement a "nested loop" over the keys and each of its values. Decide on your own:
int main() {
std::multimap<int, std::string> m;
m.insert({1,"First0"});
m.insert({1,"First0"});
m.insert({1,"First1"});
m.insert({3,"Third"});
for (auto& p : m) {
auto key = p.first;
auto val = p.second;
cout << key << ":" << val << endl;
}
std::map<int,std::set<std::string>> m2;
m2[1].insert("First0");
m2[1].insert("First0");
m2[1].insert("First1");
m2[3].insert("Third");
for (auto& p : m2) {
auto key = p.first;
auto set = p.second;
cout << key << ":" << endl;
for (auto &val : set) {
cout << " " << val << endl;
}
}
}
Output:
1:First0
1:First0
1:First1
3:Third
1:
First0
First1
3:
Third

How to insert a struct into a set and print the members of the set? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have to use the struct mov
struct mov {
string src;
string dst;
};
where src is the source and dst is the destination. The purpose of the program is to analyze the pieces on a chessboard and generate all of the possible moves. The possible moves must be represented in a set but it must be a set of moves, so set. I've found some methods saying to implement a comparator but I have no clue if it works because when printing the set (using an iterator) I get errors because of the "<<" when printing I guess its conflicting with the comparator since it uses "<"???
<< and < are never confused. Packing mov members and using the fact that std::tuple implements operator< as a lexicographical ordering, you can easily write a comparator of mov as follows:
struct mov
{
std::string src;
std::string dst;
bool operator<(const mov& rhs) const {
return std::tie(src, dst) < std::tie(rhs.src, rhs.dst);
}
};
Then this works with std::set as follows. DEMO is here.
int main()
{
std::set<mov> moves{ {"src1","dts1"}, {"src2","dts2"}, {"src3","dts3"} };
// iterator
std::cout << "Using iterator," << std::endl;
for(auto it = moves.begin(); it != moves.cend(); ++it){
std::cout << it->src << "," << it->dst << std::endl;
}
std::cout << std::endl;
// range-based-for
std::cout << "Using range-based-for," << std::endl;
for(const auto& mov_i : moves){
std::cout << mov_i.src << "," << mov_i.dst << std::endl;
}
return 0;
}

Boost log incorrect log [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have a easy struct
struct Test {
std::vector<int> values;
int value;
}
with overloaded << operator
inline std::ostream& operator<<(std::ostream& p, const Test& t)
{
p << "test: ";
for(size_t i = 0; i < t.values.size(); i++) {
std::cout << t.values[i] << " ";
}
p << " value: " << t.value << std::endl;
return p;
}
this works fine when i use the default output. But when i am using my boost logge, shown here Different boost log sinks for every class, it print the values inside my console and the rest inside my file. Anyone has an idea what happens there?
std::cout << t.values[i] << " ";
should be
p << t.values[i] << " ";