These are two pieces of code that I ran under the C++11 standard. I expected the post-decrement of the iterator to produce the same effect, but these two pieces of code produce completely different results. Where is my understanding off?
list<int> L;
int main() {
L.push_back(0);
L.push_front(1);
auto it = L.begin();
for (int i = 2; i <= 5; i++) {
L.insert(it--, i);
}
for (auto num : L) {
printf("%d ", num);
}
// 2 5 0 4 1 3
}
list<int> L;
int main() {
L.push_back(0);
L.push_front(1);
auto it = L.begin();
for (int i = 2; i <= 5; i++) {
L.insert(it, i);
it--;
}
for (auto num : L) {
printf("%d ", num);
}
// 5 4 3 2 1 0
}