C++ 为什么我不能取消引用迭代器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9141123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:22:09  来源:igfitidea点击:

why can't I dereference an iterator?

c++iterator

提问by SirYakalot

If I have a vector of objects (plain objects, not pointers or references), why can't I do this?

如果我有一个对象向量(普通对象,而不是指针或引用),为什么我不能这样做?

Object* object;

object = vectorOfObjects.end();

or

或者

object = &vectorOfObjects.end();

or

或者

object = &(*vectorOfObjects.end());

also the same question if 'object' was a reference.

如果 'object' 是一个参考,同样的问题。

回答by jalf

They are three separate errors:

它们是三个独立的错误:

object = vectorOfObjects.end();

won't work because end()returns a an iterator, and objectis a pointer. Those are generally distinct types (A vector canuse raw pointers as iterators, but not all implementations do it. Other containers require special iterator types).

不会工作,因为end()返回一个迭代器,并且object是一个指针。这些通常是不同的类型(向量可以使用原始指针作为迭代器,但并非所有实现都这样做。其他容器需要特殊的迭代器类型)。

object = &vectorOfObjects.end();

doesn't work because you take the address of the returned iterator. That is, you get a pointer to an iterator, rather than a pointer to an Object.

不起作用,因为您采用了返回的迭代器的地址。也就是说,您获得的是指向迭代器的指针,而不是指向Object.

object = &(*vectorOfObjects.end());

doesn't work because the enditerator doesn't point to a valid element. It points one past the end of the sequence. And so, it can't be dereferenced. You can dereference the last element in the sequence (which would be --vectorOfObjects.end()), but not an iterator pointing pastthe end.

不起作用,因为end迭代器未指向有效元素。它指向序列末尾的一个。因此,它不能被取消引用。您可以取消引用序列中的最后一个元素(这是--vectorOfObjects.end()),但不是一个迭代器,指向过去的结束。

Finally, the underlying problem/confusion might be that you think an iterator can be converted to a pointer. In general, it can't. If your container is a vector, you're guaranteed that elements are allocated contiguously, like in an array, and so a pointer will work. But for, say, a list, a pointer to an element is useless. It doesn't give you any way to reach the nextelement.

最后,潜在的问题/困惑可能是您认为迭代器可以转换为指针。一般来说,不能。如果您的容器是向量,则可以保证元素是连续分配的,就像在数组中一样,因此指针将起作用。但是对于 a 来说 list,指向元素的指针是没有用的。它没有给你任何到达下一个元素的方法。

回答by Luchian Grigore

Because .end()returns an iterator, not an Object, which isn't even a valid member of the vector.

因为.end()返回一个迭代器,而不是一个Object,它甚至不是向量的有效成员。

Even using begin(), which returns a valid object, you'd need the following:

即使使用begin()返回有效对象的 ,您也需要以下内容:

The correct way would be:

正确的方法是:

std::vector<Object> vec;
std::vector<Object>::iterator it;
//....
it = vec.begin();
Object o = *it;

回答by dasblinkenlight

This is because by convention enddoes not point to a valid location in the container: it sits at a location one past the end of the container, so dereferencing it is illegal.

这是因为按照惯例end并不指向容器中的有效位置:它位于容器末尾的位置,因此取消引用它是非法的。

The situation with begin()is different: you can dereference it when v.begin() != v.end()(i.e. when the container is not empty):

情况begin()不同:您可以在以下情况下取消引用它v.begin() != v.end()(即当容器不为空时):

object = *vectorOfObjects.begin();

If you need to access the last element, you can do this:

如果您需要访问最后一个元素,您可以这样做:

vector<object>::const_iterator i = vectorOfObjects.end();
i--;
cout << *i << endl; // Prints the last element of the container

Again, this works only when your vector contains at least one element.

同样,这仅在您的向量包含至少一个元素时才有效。

回答by msgmash.com

Object* means a pointer to an Object, where a pointer is a memory address. But the .end() method returns an Iterator, which is an object itself, not a memory address.

Object* 表示指向 Object 的指针,其中指针是内存地址。但是 .end() 方法返回一个 Iterator,它是一个对象本身,而不是一个内存地址。