为什么我不能取消引用迭代器?
如果我有一个对象向量(普通对象,而不是指针或引用),为什么我不能这样做?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为
.end()
返回一个迭代器,而不是一个Object
,它甚至不是向量的有效成员。即使使用返回有效对象的
begin()
,您也需要以下内容:正确的方法是:
Because
.end()
returns an iterator, not anObject
, 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:
这是因为按照惯例
end
并不指向容器中的有效位置:它位于容器末尾之后的位置,因此取消引用它是非法的。begin()
的情况有所不同:当v.begin() != v.end()
时(即当容器不为空时),您可以取消引用它:如果您需要访问最后一个元素,您可以这样做:
同样,这仅当您的向量包含至少一个元素时才有效。
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 whenv.begin() != v.end()
(i.e. when the container is not empty):If you need to access the last element, you can do this:
Again, this works only when your vector contains at least one element.
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.
它们是三个独立的错误:
不起作用,因为
end()
返回一个迭代器,而object
是一个指针。这些通常是不同的类型(向量可以使用原始指针作为迭代器,但并非所有实现都这样做。其他容器需要特殊的迭代器类型)。不起作用,因为您获取了返回的迭代器的地址。也就是说,您将获得一个指向迭代器的指针,而不是指向
Object
的指针。不起作用,因为
end
迭代器没有指向有效元素。它指向序列末尾的一个。因此,它不能被取消引用。您可以取消引用序列中的最后一个元素(即--vectorOfObjects.end()
),但不能引用指向末尾的迭代器。最后,潜在的问题/困惑可能是您认为迭代器可以转换为指针。一般来说,不能。如果您的容器是一个向量,则可以保证元素是连续分配的,就像在数组中一样,因此指针将起作用。但对于
列表
来说,指向元素的指针是没有用的。它不会为您提供任何到达下一个元素的方法。They are three separate errors:
won't work because
end()
returns a an iterator, andobject
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).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
.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.