简体   繁体   English

如何将“const_reverse_iterator”参数传递给“std :: vector :: erase()”?

[英]How do I pass a “const_reverse_iterator” parameter to “std::vector::erase()”?

std::vector::erase() does not accept reverse iterator. std::vector::erase()不接受反向迭代器。
Is there any way to call this method with a reverse iterator? 有没有办法用反向迭代器调用这个方法?

My sample code is: 我的示例代码是:

std::vector<int> MyVector;
for (int i=0; i<10; i++)
{
    MyVector.push_back(i);
}
// Now suppose that I want to erase the last three elements
int nEraseCount = 0;
for (std::vector<int>::const_reverse_iterator it=MyVector.rbegin();
        it<MyVector.rend(); ++it)
{
    MyVector.erase(it);
    if (++nEraseCount == 3) break;
}

However, this sample code is not working, because it is a reverse iterator and erase() does not take reverse iterator as its argument. 但是,此示例代码不起作用,因为it是反向迭代器,而erase()不将反向迭代器作为其参数。

How do I modify this code so that it works? 如何修改此代码以使其有效?

You can convert from reverse_iterators to iterators using base() although you need to subtract one to get the one that points to the same element thus rbegin() points to end() and rend() points to begin() (because it is not possible to point to one before the beginning in reality). 您可以使用base()从reverse_iterators转换为迭代器,尽管您需要减去一个以获得指向同一元素的那个,因此rbegin()指向end()和rend()指向begin()(因为它不是在现实中可能指向一个)。

You have more of a problem because you are using a const_reverse_iterator which cannot be converted to a non-const one and erase requires non-const iterators. 你有更多的问题,因为你使用的const_reverse_iterator不能转换为非const的,而erase需要非const迭代器。 The logic is that you are modifying the collection so you use a non-const iterator. 逻辑是您正在修改集合,因此您使用非const迭代器。

In your case, you have a bigger problem with your loop as you are removing iterators thus invalidating them, then trying to move back to the previous element. 在你的情况下,你的循环有一个更大的问题,因为你正在删除迭代器,从而使它们无效,然后尝试移回到前一个元素。

If you need to remove the last 3 elements then you should use an erase method that takes a range rather than remove them one at a time. 如果你需要删除最后3个元素,那么你应该使用一个擦除方法,它取一个范围而不是一次删除一个。

This can be done using MyVector.erase(MyVector.rbegin() + 3).base(), MyVector.end() ) in this particular case as long as you know that MyVector.size() >= 3 只要您知道MyVector.size() >= 3 ,就可以在此特定情况下使用MyVector.erase(MyVector.rbegin() + 3).base(), MyVector.end() )来完成此操作。

I would workaround the problem by not using a reverse iterator. 我会通过不使用反向迭代器解决问题。 I'll probably write something like that: 我可能会写这样的东西:

std::vector<int> MyVector;
for (int i=0; i<10; i++)
{
    MyVector.push_back(i);
}
// Now suppose that I want to erase the last three elements
int nEraseCount = 0;
while (nEraseCount < 3 && !MyVector.empty())
{
    MyVector.pop_back();
    ++nEraseCount;
}

Okay you have several options - you are erasing from the end - so you could: 好的,你有几种选择 - 你从最后删除 - 所以你可以:

resize()

if (MyVector.size() > 3)
  MyVector.resize(MyVector.size() - 3);
else
  MyVector.clear(); // presumably you don't want all anyway!

simple difference 简单的区别

if (MyVector.size() > 3)
  MyVector.erase(MyVector.end() - 3, MyVector.end());
else
  MyVector.clear(); // presumably you don't want all anyway!

The approach you've taken is not very idiomatic 你采取的方法并不是非常惯用的

If you just want to remove N elements at the back: 如果您只想删除背面的N个元素:

size_t N = 3;
size_t to_remove = std::min(vec.size(), N);
vec.erase(vec.end() - to_remove, vec.end());

You cannot pass const_iterator or const_reverse_iterator to erase() , as it is readonly iterator! 你不能将const_iteratorconst_reverse_iterator传递给erase() ,因为它只是readonly迭代器!

You should use non-const forward iterator version : std::vector<int>::iterator . 您应该使用非const 前向迭代器版本: std::vector<int>::iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++中将DRY原则应用于迭代器? (iterator,const_iterator,reverse_iterator,const_reverse_iterator) - How do I apply the DRY principle to iterators in C++? (iterator, const_iterator, reverse_iterator, const_reverse_iterator) 如何检查const_reverse_iterator到reverse_iterator的赋值是无效的? - How can I check that assignment of const_reverse_iterator to reverse_iterator is invalid? 如何使用std :: reverse_iterator擦除* AND CONTINUE *? - How do you erase *AND CONTINUE* using a std::reverse_iterator? 为什么将std :: vector :: erase参数更改为const_iterator? - Why were the std::vector::erase parameters changed to const_iterator? 使用reverse_iterator而不是const_reverse_iterator并获得令人讨厌的编译器警告和错误 - Using reverse_iterator instead of const_reverse_iterator and getting nasty compiler warnings and errors vector :: erase和reverse_iterator - vector::erase and reverse_iterator 如何获得 ::iterator of const std::vector<T> - How to get ::iterator of const std::vector<T> 如何正确使用 std:vector 的 erase() function? - How do I use std:vector's erase() function properly? 如何按索引从 std::vector<> 中删除元素? - How do I erase an element from std::vector<> by index? 如何使用来自std :: set的反向迭代器擦除元素? - How to erase element with a reverse iterator from a `std::set`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM