[英]erasing one vector of a 2d vector c++ by judging an element of that vector
vector< vector<int> > 2d_vector;
here 2d_vector is an*3 vector where n indicates the element of it, eg 2d_vector = { {0,0,2}, {0,0,1}, {0,0,0}, {0,0,-1} } 这里2d_vector是一个* 3向量,其中n表示它的元素,例如2d_vector = {{0,0,2},{0,0,1},{0,0,0},{0,0,-1 }}
I was trying to erase elements of this 2d_vector that fit "2d_vector[i][2] == -1", where i is from 0 to n. 我试图删除适合“ 2d_vector [i] [2] == -1”的2d_vector元素,其中i从0到n。 My code is as follows:
我的代码如下:
vector< vector<int> >::iterator it = 2d_vector.begin();
for( ;it<2d_vector.end();it+=3){
if(**(it+2) == -1){
it = staticBlocks.erase(it);
}
}
But it does not work. 但这行不通。 How should I do?
我应该怎么做? Thank you in advance.
先感谢您。
Let's break down **(it+2)
. 让我们分解
**(it+2)
。 it+2
refers to the second vector
after the one referenced by it
. it+2
指第二vector
的一个通过引用后it
。 *(it+2)
dereferences the iterator and gets the second vector
after the one referenced by it
. *(it+2)
解引用迭代器并获取第二vector
的一个通过引用后it
。 This is a vector
, not a pointer or an iterator. 这是一个
vector
,而不是指针或迭代器。 It cannot be dereferenced, so **(it+2)
is doomed. 无法取消引用,因此注定
**(it+2)
。 However, (*(it+2))[0]
Should do what you appear to want. 但是,
(*(it+2))[0]
应该可以执行您想要的操作。
This assumes it+2
is within range. 假定
it+2
在范围内。
You meant that kind of thing? 你是说那种话 :
:
std::vector<std::vector<int>> vec;
for (std::vector<std::vector<int>>::iterator it = vec.begin(); it != vec.end(); ++it) {
if (it->at(2) == -1){
vec.erase(it);
--it; // This may fix it, if a vector gets deleted, the rest of the list goes one step towards the beggining of it and the next vector will be skipped
}
}
It fits to this 适合这个
erase elements of this 2d_vector that fit "2d_vector[i][2] == -1",
删除此2d_vector符合“ 2d_vector [i] [2] == -1”的元素,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.