亲爱的大家, 我使用了向量向量,比如vector<vector<int> > no_1_2来存储两个vector<int>容器的元素,比如说no1和no2。 所以我想说,no1 = {2,5,7,10,3}和no2 = {21,34,15}。
我想将这两个向量存储在一个名为no_1_2的容器中,我在两个'for'循环中使用了no_1_2[0].push_back(no1.at(i))和no_1_2[1].push_back(no2.at(j)) 。 但是,我得到了错误。 无论如何要解决这个问题?
我不能push_back矢量矢量? 请帮忙..谢谢
对不起,这是我的代码的一部分,//一些类和一些代码在这里
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 }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 }最满意答案
没有看到您的实际代码和错误,让我猜:
在您访问no_1_2[0]或no_1_2[1]之前,您没有致电no_1_2.resize(2)
编辑:看到代码后,上面不再猜测:
你不能做no_1_2[0].push_back(*i); - no_1_2为空,您尝试访问no_1_2[0] 。 您需要在循环之前调整它的大小。
EDIT2:
在循环外移动排序/唯一/擦除,并使用assign或operator =:
sort(in.begin(),in.end()); in.erase(unique(in.begin(),in.end()),in.end()); no_1_2[0].assign(in.begin(), in.end()); // OR: no_1_2[0] = in;Without seeing your actual code and errors, let me guess:
You didn't call no_1_2.resize(2) before you accessed no_1_2[0] or no_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_2 is empty and you're trying to access no_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=:
sort(in.begin(),in.end()); in.erase(unique(in.begin(),in.end()),in.end()); no_1_2[0].assign(in.begin(), in.end()); // OR: no_1_2[0] = in;更多推荐
发布评论