【问题标题】:In this C++ code, what element of the list does the iterator refer to? [closed]在这段 C++ 代码中,迭代器引用列表的哪个元素? [关闭]
【发布时间】:2018-04-03 04:50:31
【问题描述】:

代码如下:

listOfFoos::iterator new_foo_it = listOfFoos.insert(listOfFoos.end(),newFoo);

listOfFoos 就是这样,std::listfoo 对象。通常你会写:

listOfFoos::iterator new_foo_it = listOfFoos.begin();

我也明白list.insert()会在指定索引处插入newFoo,然后迭代器就会被创建。但我不知道插入如何影响迭代器的位置。

【问题讨论】:

  • 如果您阅读了 list::insert 文档,这完全是您自己可以找到的。
  • 重新运行一个指向第一个插入元素的迭代器。你可以在这里阅读:cplusplus.com/reference/list/list/insert 或在这里:en.cppreference.com/w/cpp/container/list/insert 我建议使用其中一页作为 C++ 参考。两者都不错,使用要根据自己的喜好而定。对于在 stackoverflow 上提出此类问题,您将获得负分,因为此答案可以在语言参考中找到
  • 感谢您的链接。我确实阅读了 cplusplus.com 上的文档,但我没有正确解释它。对于给您带来的麻烦,我深表歉意。

标签: c++ listiterator


【解决方案1】:

手册中有详细记录。

指向第一个新插入元素的迭代器。

在您的情况下,new_foo_it 在单个元素的 listOfFoos.insert 之后,指向列表的最后一个元素,它指向 newFoo

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
相关资源
最近更新 更多