c++ - vector 排序是否会使迭代器无效?

标签 c++ sorting stl

 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?

最佳答案

根据 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 节“排序和相关操作”未指定迭代器将失效,因此问题中的迭代器应保持有效。

关于c++ - vector 排序是否会使迭代器无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3885482/

相关文章:

c++ - 在 android 内核开发 linux 中使用蓝牙

c++ - 使用 Arduino Due 从 MPU-6050 获取/处理正确的值

c++ - 返回包含输入容器特定元素的容器的函数

c++ - 在我的特殊情况下我应该使用 vector 还是集合?或者完全不同的东西?

c++ - 当有人尝试在当前 .cpp 或 .h 文件中使用 STL 时显示错误(在 Visual Studio 中)

c++ - 添加新节点后链表头始终为空

javascript - 数组在另一个值中按字母顺序排序在 Javascript 中排序

java - 如何使用java从文本文件中删除一行?

javascript - 如何使用 javascript 让文本出现在文本字段中?

c++ - 什么 C++0x 特性会产生最大的影响?