简体   繁体   English

向量排序是否使迭代器无效?

[英]Does a vector sort invalidate iterators?

 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?

According to the C++ Standard §23.1/11: 根据C ++标准§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. §25.3“排序和相关操作”没有指定迭代器将被无效,因此问题中的迭代器应该保持有效。

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. 向量的那些插槽中的值可能已更改,但每个驻留的存储位置保持不变。

std::sort will not invalidate iterators to a vector. std::sort不会使向量的迭代器失效。 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? ): 除了对Kirill V. Lyadvinsky提供的标准的支持( 矢量排序是否使迭代器失效? ):

  • 25/5 "Algorithms library" 25/5“算法库”

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)。

  • 24.1/4 "Iterator requirements" 24.1 / 4“迭代器要求”

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. 除了它的类别之外,正向,双向或随机访问迭代器也可以是可变的或常量的,这取决于表达式* i的结果是作为引用还是作为对常量的引用。

std::vector keeps its elements in contiguous memory. std::vector将其元素保存在连续的内存中。 std::sort takes arguments (iterators) by value and re-arranges the sequence between them. std::sort按值获取参数(迭代器)并重新排列它们之间的序列。 The net result is your local variables start and end are still pointing to first and one-past-the-last elements of the vector. 最终结果是你的局部变量startend仍然指向向量的第一个和最后一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM