I have used the new range-based for loop provided by C++11 standard and I came up with the following question: suppose that we iterate over a vector<> using the range-based for, and we add some element in the end of the vector during this iteration. Thus, when do the loop end?For instance, see this code:#include <iostream>#include <vector>using namespace std;int main() { vector<unsigned> test({1,2,3}); for(auto &num : test) { cout << num << " "; if(num % 2) test.push_back(num + 10); } cout << ""; for(auto &num : test) cout << num << " "; return 0;}I tested G++ 4.8 and Apple LLVM version 4.2 (clang++) with "-std=c++11" flag, and the output is (for both):1 2 31 2 3 11 13Note that the first loop terminates in the end of original vector, although we add other elements to it. It seems that the for-range loop evaluate the container end in beginning only.Is this, in fact, the correct behavior of range-for? Is it specified by the committee? Can we trust in this behavior?Note that if we change the first loop byfor(vector<unsigned>::iterator it = test.begin(); it != test.end(); ++it)with invalid the iterators and come up with a segmentation fault. 解决方案 No you cannot rely on this behaviour. Modifying the vector inside the loop results in undefined behaviour because the iterators used by the loop are invalidated when the vector is modified.The range based for loop for ( range_declaration : range_expression) loop_statementis essentially equivalent to { auto && __range = range_expression ; for (auto __begin = std::begin(__range), __end = std::end(__range); __begin != __end; ++__begin) { range_declaration = *__begin; loop_statement }}When you modify the vector, the iterators __begin and __end are no longer valid and the dereferencing __begin results in undefined behaviour. 这篇关于在基于范围的循环 c++11 期间将元素添加到向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!