fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <memory>
  5. #include <map>
  6. #include <vector>
  7. #include <utility>
  8. #include <string>
  9. #include <locale>
  10. #include <list>
  11. using namespace std;
  12.  
  13.  
  14. int main(void)
  15. {
  16. #define DBG(x) cout << left << setw(30) << #x << boolalpha << (x) << endl;
  17.  
  18.  
  19. list<string> l = { "one", "two", "three", "four" };
  20.  
  21. auto one_it = begin(l);
  22.  
  23. l.emplace_front("zero"); // doesn't invalidate any iterators
  24. l.emplace_front("minus one");
  25. l.emplace_front("minus two");
  26. l.emplace_front("minus three");
  27.  
  28.  
  29. l.erase(one_it); // O(1)
  30.  
  31. for(auto const& s : l) DBG(s);
  32.  
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
s                             minus three
s                             minus two
s                             minus one
s                             zero
s                             two
s                             three
s                             four