[英]vector.erase and std::remove on custom vector
I have a vector of int pairs. 我有一个整数对向量。 Assuming Itr as my iterator for this vector.
假设Itr是此向量的迭代器。 I want to iterate over the vector and make a decision whether or not to remove the element from the vector.
我想遍历向量,并决定是否从向量中删除元素。 If the element of the vector is
9001,3
then i want to remove all the elements from the vector whose itr->first
is 9001(irrespective or what itr->second
is). 如果向量的元素是
9001,3
那么我想从向量中删除所有itr->first
为9001(无论是什么itr->second
是)的元素。
Questions: 问题:
vec.erase(std::remove(vec.begin(), vec.end(), Itr->first=9001), vec.end());
vec.erase(std::remove(vec.begin(), vec.end(), Itr->first=9001), vec.end());
(current element being pointer by vector) - 10
and ve.end as (current element being pointed by vector)+10
? (current element being pointer by vector) - 10
和ve.end作为(current element being pointed by vector)+10
吗? Example: vec.erase(std::remove(Itr-10, Itr+10, Itr->first=9001), vec.end());
例如:
vec.erase(std::remove(Itr-10, Itr+10, Itr->first=9001), vec.end());
It might quiet be possible that Itr+10 ot Itr-10 will result in a segmentation fault if the vector size itself is less than 10. So how to deal with this situation? 如果向量大小本身小于10,则Itr + 10或Itr-10可能会导致分段错误,这很安静。那么如何处理这种情况?
vector<pair<int,int> > vec;
vec.push_back(pair<int,int>(9001,1));
vec.push_back(pair<int,int>(9001,2));
vec.push_back(pair<int,int>(9001,3));
vec.push_back(pair<int,int>(9001,4));
vec.push_back(pair<int,int>(9002,1));
vec.push_back(pair<int,int>(9002,2));
vec.push_back(pair<int,int>(9002,3));
vec.push_back(pair<int,int>(9002,4));
vec.push_back(pair<int,int>(9002,5));
vector<pair<int,int> >::iterator Itr;
for(Itr=vec.begin();Itr!=vec.end();++Itr)
cout<<vecItr->first<<" "<<vecItr->second;
// vec.erase(std::remove(Itr-10, Itr+10, Itr->first=9001), vec.end()); //This doest work
for(Itr=vec.begin();Itr!=vec.end();++Itr)
cout<<Itr->first<<" "<<Itr->second;
How do i remove this vector of int pairs.
我如何删除此整数对向量。 The following wouldn't work:
以下内容不起作用:
Use lambda or custom comparator with remove_if
algorithm: 将lambda或自定义比较器与
remove_if
算法一起使用:
vec.erase(std::remove_if(vec.begin(), vec.end(),
[](auto& elem){ return elem.first == 9001;} ),
vec.end());
Using custom comparator: 使用自定义比较器:
struct elem_equals
{
typedef std::pair<int,int> elem_t
const int value;
elem_equals(int v) : value(v) {}
bool operator()(elem_t& elem)
{
return elem.first == value;
}
};
//...
vec.erase(std::remove_if(vec.begin(), vec.end(),
elem_equals(9001) ),
vec.end());
Instead of giving a range as vec.begin() to vec.end() is it possible for me to give vec.begin as
(current element being pointer by vector) - 10
andve.end as (current element being pointed by vector)+10
?代替给vec.begin()到vec.end()的范围,我可以给vec.begin作为
(current element being pointer by vector) - 10
和ve.end as (current element being pointed by vector)+10
吗?
Yes. 是。 Vector iterator support pointer arithmetics, so it is easy.
向量迭代器支持指针算术,因此很容易。
It might quiet be possible that Itr+10 ot Itr-10 will result in a segmentation fault if the vector size itself is less than 10. So how to deal with this situation?
如果向量大小本身小于10,则Itr + 10或Itr-10可能会导致分段错误,这很安静。那么如何处理这种情况?
Clamp your range if there is not enough elements before or after iterator: 如果在迭代器之前或之后没有足够的元素,请固定范围:
//iter is an iterator to vector
//vec is instance of std::vector
//replace auto with std::vector<std::pair<int,int> >::iterator for non C++11 compilers
auto begin = iter - std::min(10, iter - vec.begin());
auto end = iter + std::min(10, vec.end() - iter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.