向量排序会使迭代器无效吗?

发布于 2024-09-26 21:38:44 字数 286 浏览 0 评论 0原文

 std::vector<string> names;
 std::vector<string>::iterator start = names.begin();
 std::vector<string>::iterator end = names.end();
 sort (start,end);
 //are my start and end valid at this point?
 //or they do not point to front and tail resp?
 std::vector<string> names;
 std::vector<string>::iterator start = names.begin();
 std::vector<string>::iterator end = names.end();
 sort (start,end);
 //are my start and end valid at this point?
 //or they do not point to front and tail resp?

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

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

发布评论

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

评论(4

根据 C++ 标准 §23.1/11:

除非另有说明(明确指定或通过根据其他函数定义函数),调用
容器成员函数或将容器作为参数传递给库函数不应无效
迭代器或更改该容器内的对象的值。

§25.3“排序和相关操作”没有指定迭代器将无效,因此问题中的迭代器应该保持有效。

According to the C++ Standard §23.1/11:

Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking
a container member function or passing a container as an argument to a library function shall not invalidate
iterators
to, or change the values of, objects within that container.

§25.3 "Sorting and related operations" doesn't specify that iterators will be invalidated, so iterators in the question should stay valid.

渡你暖光 2024-10-03 21:38:44

它们仍然指向开始和结束。向量的这些槽中的值可能已更改,但每个槽所在的存储位置保持不变。

They still point to the beginning and end. The values in those slots of the vector have probably changed, but the storage location in which each resides remains the same.

若能看破又如何 2024-10-03 21:38:44

std::sort 不会使向量的迭代器无效。排序模板在迭代器上使用 * 运算符来访问和修改向量的内容,并且通过迭代器修改向量元素到向量中已有的元素不会使任何迭代器无效。

总之,

  • 您现有的迭代器不会失效
  • ,但是它们指向的元素可能已被修改

除了对 Kirill V. Lyadvinsky 提供的标准的支持 (向量排序是否会使迭代器无效?):

  • 25 /5“算法库”

如果算法的效果部分说
任何指向的值
作为参数传递的迭代器是
修改后,那么该算法有一个
附加类型要求:类型
该论点应满足
可变迭代器的要求
(24.1)。

  • 24.1/4“迭代器要求”

除了它的类别之外,前锋,
双向或随机访问
迭代器也可以是可变的或
常数取决于是否
表达式 *i 的结果表现为
参考或作为参考
常数。

std::sort will not invalidate iterators to a vector. The sort template uses the * operator on the iterators to access and modify the contents of the vector, and modifying a vector element though an iterator to an element already in the vector will not invalidate any iterators.

In summary,

  • your existing iterators will not be invalidated
  • however, the elements they point to may have been modified

In addition to the support for the standard provided by Kirill V. Lyadvinsky (Does a vector sort invalidate iterators?):

  • 25/5 "Algorithms library"

If an algorithm’s Effects section says
that a value pointed to by any
iterator passed as an argument is
modified, then that algorithm has an
additional type requirement: The type
of that argument shall satisfy the
requirements of a mutable iterator
(24.1).

  • 24.1/4 "Iterator requirements"

Besides its category, a forward,
bidirectional, or random access
iterator can also be mutable or
constant depending on whether the
result of the expression *i behaves as
a reference or as a reference to a
constant.

够钟 2024-10-03 21:38:44

std::vector 将其元素保存在连续的内存中。 std::sort 按值获取参数(迭代器)并重新排列它们之间的顺序。最终结果是局部变量 startend 仍然指向向量的第一个和最后一个元素。

std::vector keeps its elements in contiguous memory. std::sort takes arguments (iterators) by value and re-arranges the sequence between them. The net result is your local variables start and end are still pointing to first and one-past-the-last elements of the vector.

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