如何从 STL 的 const_iterator 中获取数据?

发布于 2024-08-23 07:25:07 字数 313 浏览 6 评论 0原文

我有这样运行的东西:

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 技术交流群。

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

发布评论

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

评论(6

情定在深秋 2024-08-30 07:25:07

迭代器有一个“看起来”像指针的接口(但它们不一定是指针,所以不要用这个比喻太过分)。

迭代器表示对容器中单个数据的引用。您想要的是在迭代器指定的位置访问容器的内容。您可以使用*it 访问it 位置的内容。同样,您可以使用 it->method() 对位于 it 位置的内容(如果内容是对象)调用方法。

这与您的问题并不真正相关,但这是一个需要注意的常见错误(即使我仍然时不时地犯):如果位置 it 处的内容是 < em>指向对象的指针,要调用对象的方法,语法为(*i​​t)->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 position it (if the contents are an object) using it->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.

葬花如无物 2024-08-30 07:25:07

使用迭代器的语法与使用指针的语法基本相同。要获取迭代器“指向”的值,您可以使用 * 进行取消引用:

 if (*it == baseline) ...

如果列表是对象列表,您还可以使用 ->< 访问对象的方法和属性/代码>:

 if (it->someValue == baseline) ...

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 (*it == baseline) ...

If the list is a list of objects you can also access methods and properties of the objects with ->:

 if (it->someValue == baseline) ...
℉絮湮 2024-08-30 07:25:07

使用:

if (*it == baseline) 

Use:

if (*it == baseline) 
月亮是我掰弯的 2024-08-30 07:25:07

使用 *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)

披肩女神 2024-08-30 07:25:07

std 迭代器重载了operator*(),这样您就可以像访问指针一样访问引用的位置。

T const& a = *it;

the std iterators overload the operator*(), this way you can access the referenced location the same way as if it was a pointer.

T const& a = *it;
阪姬 2024-08-30 07:25:07

据我所知,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/

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