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

发布于 2025-01-02 00:50:57 字数 296 浏览 2 评论 0原文

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

Object* object;

object = vectorOfObjects.end();

或者

object = &vectorOfObjects.end();

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

如果“对象”是引用,那么还有同样的问题

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

べ映画 2025-01-09 00:50:58

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

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

正确的方法是:

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

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

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

The correct way would be:

std::vector<Object> vec;
std::vector<Object>::iterator it;
//....
it = vec.begin();
Object o = *it;
久隐师 2025-01-09 00:50:58

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

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

object = *vectorOfObjects.begin();

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

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

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

This is because by convention end does 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.

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

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.

梦里南柯 2025-01-09 00:50:58

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

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.

淡墨 2025-01-09 00:50:57

它们是三个独立的错误:

object = vectorOfObjects.end();

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

object = &vectorOfObjects.end();

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

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

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

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

They are three separate errors:

object = vectorOfObjects.end();

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

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 = &(*vectorOfObjects.end());

doesn't work because the end iterator 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 past the 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 next element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文