如何从 STL 的 const_iterator 中获取数据?
我有这样运行的东西:
T baseline;
list<T>::const_iterator it = mylist.begin();
while (it != mylist.end()) {
if (it == baseline) /* <----- This is what I want to make happen */
// do stuff
}
我的问题是我不知道如何从迭代器中提取数据。我觉得这是一件愚蠢的事情,令人困惑,但我不知道该怎么做。
编辑:修复了 begin.end()
I have something that runs like this:
T baseline;
list<T>::const_iterator it = mylist.begin();
while (it != mylist.end()) {
if (it == baseline) /* <----- This is what I want to make happen */
// do stuff
}
My problem is that I have no idea how to extract the data from the iterator. I feel like this is a stupid thing to be confused about, but I have no idea how to do it.
EDIT : Fixed begin.end()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
迭代器有一个“看起来”像指针的接口(但它们不一定是指针,所以不要用这个比喻太过分)。
迭代器表示对容器中单个数据的引用。您想要的是在迭代器指定的位置访问容器的内容。您可以使用
*it
访问it
位置的内容。同样,您可以使用it->method()
对位于it
位置的内容(如果内容是对象)调用方法。这与您的问题并不真正相关,但这是一个需要注意的常见错误(即使我仍然时不时地犯):如果位置
it
处的内容是 < em>指向对象的指针,要调用对象的方法,语法为(*it)->method()
,因为有两级间接。Iterators have an interface that "looks" like a pointer (but they are not necessarily pointers, so don't take this metaphor too far).
An iterator represents a reference to a single piece of data in a container. What you want is to access the contents of the container at the position designated by the iterator. You can access the contents at position
it
using*it
. Similarly, you can call methods on the contents at positionit
(if the contents are an object) usingit->method()
.This doesn't really relate to your question, but it is a common mistake to be on the lookout for (even I still make it from time to time): If the contents at position
it
are a pointer to an object, to call methods on the object, the syntax is(*it)->method()
, since there are two levels of indirection.使用迭代器的语法与使用指针的语法基本相同。要获取迭代器“指向”的值,您可以使用
*
进行取消引用:如果列表是对象列表,您还可以使用
->< 访问对象的方法和属性/代码>:
The syntax to use an iterator is basically the same as with a pointer. To get the value the iterator "points to" you can use dereferencing with
*
:If the list is a list of objects you can also access methods and properties of the objects with
->
:使用:
Use:
使用 *it 来访问迭代器所指向的元素。当你比较时我想你应该使用 if (*it == 基线)
Use *it to access the element pointed by the iterator. When you are comparing i guess you should use if (*it == baseline)
std 迭代器重载了operator*(),这样您就可以像访问指针一样访问引用的位置。
the std iterators overload the operator*(), this way you can access the referenced location the same way as if it was a pointer.
据我所知,std 迭代器不是指针,而是指向底层单个数据元素的实际指针的薄包装。
你可以使用
某事=*它
访问迭代器指向的元素。
*it==baseline 是这种行为的正确代码。
另外,如果您正在处理 STL 中的集合,您可以考虑使用诸如
find_if
之类的函数http://www.cplusplus.com/reference/algorithm/find_if/
from what i know std iterators are not pointers, but are a thin wrapper around the actual pointers to the underlying individual data elements.
you can use
something=*it
to access an element pointed by the iterator.
The *it==baseline is a correct code for that kind of behaviour.
Also if you are dealing with collections from STL you can consider using functions such as
find_if
http://www.cplusplus.com/reference/algorithm/find_if/