[英]push_back problem on vector<vector<int> >
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. 我使用了一个向量vector,例如
vector<vector<int> > no_1_2
来存储两个vector<int>
容器的元素,例如no1&no2。 so for eg i would say, no1={2,5,7,10,3} and no2={21,34,15}. 因此例如,我会说,no1 = {2,5,7,10,3}和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. 我想将这两个向量存储在一个名为no_1_2的容器中,并且在两个“ for”循环中使用了
no_1_2[0].push_back(no1.at(i))
和no_1_2[1].push_back(no2.at(j))
。 But, i got error. 但是,我出错了。 is there anyway to solve this?
反正有解决办法吗?
Can't i push_back vector of vector? 我不能push_back 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 accessed no_1_2[0]
or no_1_2[1]
在访问
no_1_2[0]
或no_1_2[1]
之前,您没有拨打过no_1_2.resize(2)
EDIT: After seeing code, the above's no longer guesswork: 编辑:看到代码后,上述不再猜测:
You cannot do no_1_2[0].push_back(*i);
您不能执行
no_1_2[0].push_back(*i);
- no_1_2
is empty and you're trying to access no_1_2[0]
. no_1_2
为空,您正在尝试访问no_1_2[0]
。 You will need to resize it before the loop. 您需要在循环之前调整其大小。
EDIT2: 编辑2:
Move your sort/unique/erase outside the loops, and use assign or operator=: 将您的sort / unique / erase 移到循环外 ,并使用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;
std::vector::at
returns a reference to an element at position n
in the vector. std::vector::at
返回对向量中位置n
处元素的引用 。
std::vector<int> vec(2);
vec[0] = 0;
vec[1] = 1;
cout << vec.at(0) ; // returns 0 but not the vector.
So, in order to return a vector, value at the position must be a vector, which is not the case either in vectors no1
, no2
you mentioned. 因此,为了返回向量,该位置的值必须是向量,您提到的向量
no1
, no2
都不是这种情况。
std::vector< std::vector<int> > no_1_2;
std::vector<int> no1(5), no2(3) ;
// Assigning values to vector of integers no1, no2
no_1_2.push_back(no1);
no_1_2.push_back(no2);
no_1_2.at(0) ; // This returns std::vector<int>
no_1_2[0].push_back(*i);
, no_1_2[1].push_back(*o);
,
no_1_2[1].push_back(*o);
are the errors. 是错误。 You need to resize the vector size because at the point of no_1_2, in,out declarations, size of the container is not mentioned.
您需要调整向量的大小,因为在no_1_2的in,out声明中,没有提到容器的大小。
vector<vector<int>> no_1_2 ;
vector<int> in;
vector<int> out;
And since size
of no_1_2 is not mentioned, it an error to use an []
on it. 而且由于未提及no_1_2的
size
,因此在其上使用[]
是错误的。 However, once after the push_back ,it is valid though. 但是,在push_back之后 ,它仍然有效。 Example -
范例-
vector<int> temp;
temp[0] = 10; // Error : What is temp[0] ? size of temp is 0 and where would the `[0]` take to.
temp.push_back(10); // Increased the size of vector
temp[0] = 30 ; // And this is now valid.
I'm assuming that you haven't properly allocated the vector vectors. 我假设您没有正确分配向量向量。
vector< vector<int> > vectors;
vectors.resize(2);
FWIW, you're not doing a push_back of a vector of a vector, you're doing a push_back of an int from vector no2 to vector no_1_2[1]. FWIW,您不是在执行向量的向量push_back,而是在将int从向量no2转换为向量no_1_2 [1]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.