简体   繁体   English

C ++:用迭代器调用的vector :: erase

[英]c++: vector::erase called with an iterator

I am working on a vector of structs. 我正在研究结构的向量。

When I am trying to call this function with iterator, like this: 当我尝试使用迭代器调用此函数时,如下所示:

vec2.erase (vec2.begin()+iter2);

it sends me this error: 它向我发送此错误:

"no match for 'operator+' in '(+vec2)->std::vector<_Tp, _Alloc>::begin [with _Tp = wordstype, _Alloc = std::allocator<wordstype>]() + iter2'" 

Can I send an iterator (or any other parameter)? 我可以发送迭代器(或任何其他参数)吗? If yes, what is the problem? 如果是,那是什么问题?

You cannot add iterator to iterator . 您不能将iterator添加到iterator

iter2 already points to the concrete place in a vector, so you can write: iter2已经指向向量中的具体位置,因此您可以编写:

iter2 = vec2.erase(iter2);

instead. 代替。

Yes erase can work with iterator and the code will look like: 是的, erase可以与迭代器一起使用,代码如下所示:

erase(itr2);

You can also do: 您也可以:

erase(vec2.begin() + 3);

However the iterators do not declare operator+ so your call is not valid. 但是,迭代器未声明operator+因此您的调用无效。

You are trying to add together two iterators, which makes no sense. 您试图将两个迭代器加在一起,这没有任何意义。

Post your code and explain what your are trying to do. 发布您的代码并解释您要做什么。

The problem you describe doesn't seem to have anything with std::vector<>::erase , according to the error message. 根据错误消息,您描述的问题似乎与std::vector<>::erase无关。

The expression the compiler has problems with is vec2.begin() + iter2 . 编译器遇到问题的表达式是vec2.begin() + iter2 Apparently, iter2 in your case is an iterator. 显然,您的情况下iter2是一个迭代器。 vec2.begin() is also an iterator. vec2.begin()也是一个迭代器。 You cannot add one iterator to another. 您不能将一个迭代器添加到另一个。 It simply makes no sense. 这根本没有意义。 Hence the error. 因此,错误。

What are you trying to do by vec2.begin() + iter2 ? 您想通过vec2.begin() + iter2什么? If iter2 is indeed an iterator, why are you trying to add it to vec2.begin() ? 如果iter2确实是迭代器,那么为什么要尝试将其添加到vec2.begin()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM