Dear All,
I used a vector of vector, say vector<vector<int> > no_1_2 to store elements of two vector<int> containers, say no1 & no2. so for e.g. i would say, no1={2,5,7,10,3} and no2={21,34,15}.
I wanted to store these 2 vectors in one container called no_1_2 and I used no_1_2[0].push_back(no1.at(i)) and no_1_2[1].push_back(no2.at(j)) within two ‘for’ loops. But, i got error. is there anyway to solve this?
Can’t i push_back vector of vector? any help please..
thanks
sorry here is part of my code,
//some classes and some codes are here
vector< vector<int> > final_list(int code1,int code2){
vector<vector<int> > no_1_2;
vector<int> in;
vector<int> out;
for (int foo=0;foo<size();foo++){
int a_point=foo;
if (at(a_point).Code()==code1){
vector<int> closer_points;
closer_points=at(foo).Closers();
for (int fee=0;fee<closer_points.size();fee++){
int a_neb_point=closer_points.at(fee);
if (at(a_neb_point).Code()==code2){
in.push_back(a_point);
out.push_back(a_neb_point);
}
}
}
}
/remove duplicates // above in and out vectors containing some values repeatedly, so, i remove the duplicates here
for(vector<int>::iterator i=in.begin();i!=in.end();i++){
sort(in.begin(),in.end());
in.erase(unique(in.begin(),in.end()),in.end());
no_1_2[0].push_back(*i);
}
for(vector<int>::iterator o=out.begin();o!=out.end();o++){
sort(out.begin(),out.end());
out.erase(unique(out.begin(),out.end()),out.end());
no_1_2[1].push_back(*o);
}
return no_1_2;
}
int main (){
// some code
vector< vector <int> > in_out=mylist.final_list(34,1); //here i just tried for code values for (34, 1), like that i have many sets
}
Without seeing your actual code and errors, let me guess:
You didn’t call
no_1_2.resize(2)before you accessedno_1_2[0]orno_1_2[1]EDIT: After seeing code, the above’s no longer guesswork:
You cannot do
no_1_2[0].push_back(*i);–no_1_2is empty and you’re trying to accessno_1_2[0]. You will need to resize it before the loop.EDIT2:
Move your sort/unique/erase outside the loops, and use assign or operator=: