[英]Erasing an element of a vector that uses const_iterator
I have a vector of Car objects, declared as 我有一个Car对象向量,声明为
vector<Car> vCars
In one of my functions, I need to erase the first element of the vector. 在我的功能之一中,我需要擦除向量的第一个元素。 Sounds simple enough right?
听起来很简单吧? The line that is throwing the error:
引发错误的行:
vCars.erase( vCars.begin() );
And the error: 错误:
no matching function for call to 'std::vector<Car>::erase(std::vector<Car>::const_iterator) const'
I understand erase generally only takes an iterator for its parameter, and not a const_iterator. 我了解到擦除通常仅将迭代器作为其参数,而不是const_iterator。 I've been looking for solutions or workarounds to the error, such as the erase-remove idiom, but from what I'm seeing, that only removes an element by value, when I need to be removing by position - and simply enough, just the element at the first position!
我一直在寻找错误的解决方案或变通办法,例如“擦除删除”习语,但是从我所看到的情况来看,当我需要按位置删除时,只能按值删除元素-简而言之,只是元素在第一位置! (I know this is not good performance wise for a vector, but I am required to use a vector for this)
(我知道这对于矢量而言并不是很好的性能,但是我必须为此使用矢量)
edit: To clarify the situation, the function the call is contained within is below: 编辑:为了澄清这种情况,该调用所包含的函数如下:
/// Removes the Car at the front of the garage without returning the instance.
void Garage::pop() const {
if ( !empty() ) {
vCars.erase( vCars.begin() );
}
}
edit: And I now see where I went wrong. 编辑:现在我看到我出了问题。 There were a lot of methods that were const and I just mindlessly made pop() a const method!
有很多方法是const的,我只是不加思索地使pop()成为const方法! Once I removed const, the problem was resolved.
一旦删除了const,问题就解决了。 Thanks for pointing me in the right direction!
感谢您指出正确的方向!
It's not that erase
only works for iterator
s, it also works for const_iterator
s in C++11. 并不是说
erase
仅适用于iterator
,它还适用于C ++ 11中的const_iterator
。 after all, to call erase, you need a modifiable reference to the vector, and if you have that, you can always get a normal non-const iterator
from a const
one. 毕竟,要调用擦除,您需要对向量的可修改引用,并且如果有,您总是可以从
const
那里获得一个普通的非常量iterator
。 That's the rationale behind why they changed the member to take a const_iterator
. 这就是为什么他们更改成员以使用
const_iterator
。
The problem is, you only get a const_iterator
back from begin()
if the object you're calling it on is also const
qualified - in this case, your vCars
. 问题是,如果要调用的对象也是
const
限定的(在本例中为vCars
begin()
则只能从begin()
返回const_iterator
。 This, in turn, means you can only call const
qualified functions on it, which is what the compiler tries: 反过来,这意味着您只能在其上调用
const
限定函数,这是编译器尝试的操作:
... call to 'std::vector::erase(std::vector::const_iterator) const' ^^^^^
I think you agree that erase
being const
qualified wouldn't make sense. 我认为您同意将
const
erase
是没有道理的。 :) :)
The error message: 错误信息:
no matching function for call to 'std::vector::erase(std::vector::const_iterator) const'
没有匹配的函数来调用'std :: vector :: erase(std :: vector :: const_iterator)const'
implies that vCars.begin()
yields a const_iterator
, which in turn implies that vCars
is a constant object or reference. 暗示
vCars.begin()
产生一个const_iterator
,这反过来意味着vCars
是一个常量对象或引用。 You are not allowed to modify the vector through that reference. 您不允许通过该参考来修改载体。 If the function needs to modify the vector, it cannot take a constant reference.
如果函数需要修改向量,则不能采用常量引用。
Note that in a member function declared as const
the implicit this
pointer is of type T const *
(ie you cannot modify the object inside a const function). 请注意,在声明为
const
的成员函数中,隐式this
指针的类型为T const *
(即,您无法在const函数内部修改对象)。 If this is your case, you will need to drop the const
qualifier from the function. 如果是这种情况,则需要从函数中删除
const
限定符。
It seems your parameter "vCars" is a const reference to the vector. 看来您的参数“ vCars”是对向量的const引用。 You can make it mutable via const_cast or change your function design.
您可以通过const_cast使其可变或更改功能设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.