list<int> l; for (int i = 0; i < 10; ++i) l.push_back(i); auto iter = l.begin(); for (int i = -1; i >= -10; --i) l.insert(iter--, i); for (int&v: l) cout << v << ' ';
list<int> l; for (int i = 0; i < 10; ++i) l.push_back(i); auto iter = l.begin(); for (int i = -1; i >= -10; --i) { l.insert(iter, i); --iter; } for (int&v: l) cout << v << ' ';
得到的结果却是
1
-10-9-8-7-6-5-4-3-2-1 0 1 2 3 4 5 6 7 8 9
如果调整成这样
1 2 3 4 5 6 7 8
list<int> l; for (int i = 0; i < 10; ++i) l.push_back(i); auto iter = l.begin(); ++iter; for (int i = -1; i >= -10; --i) { l.insert(--iter, i); } for (int&v: l) cout << v << ' ';
voidsolve(){ for (int i = 0; i < 10; ++i) l.push_back(i); auto iter = l.begin(); for (int i = -1; i >= -10; --i) { f(iter--, i); cout << (iter == l.begin()) << endl; for (int&v: l) cout << v << ' '; cout << endl; } }