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/