2017-06-16 61 views
0

我想知道迭代進化爲什麼下面如何集裝箱()結束時(例如)集裝箱尺寸變化

end_iter_mem測試== intList.end()

還真

coliru

當容器的元素數量增加時,容器的end()「值」是否會變化?對於名單

#include <iostream> 
#include <list> 

using namespace std; 

int main() 
{ 
    list<int> intList = {1,2,3}; 

    auto end_iter_mem = intList.end(); 

    intList.push_back(4); 

    cout << (end_iter_mem == intList.end()) << endl; 

    return 0; 
} 
+0

相關:https://stackoverflow.com/questions/6438086/iterator-invalidation-rules – Rakete1111

回答

1

迭代後list::push_back操作不會失效,從cppreference

沒有迭代器或引用無效。

但這並不意味着所有容器都是如此。例如,對於vectors the documentation says

如果新的大小()比容量大(),則所有的迭代器和參考文獻(包括過去最結束迭代)被無效。否則只有最後一個迭代器失效。


文藝青年最愛的你必須諮詢文件,以瞭解迭代器是否失效和迭代器容器修改後無效。