#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
auto it = A.begin();
cout << *it << ' ';
advance(it, 1);
cout << *it << ' ';
advance(it, 1);
cout << *it << ' ';
advance(it, 1);
cout << *it << ' ';
advance(it, 1);
cout << *it << ' ';
}
I belive that list is a double linked list. Surprisingly, the output is 1 2 3 3 1. Can someone explain what's happening here? Here are questions:
- When we advance twice, we are at the node with
3. Here, an additionaladvancemoves the iterator toA.end(). I believe thatA.end()is an imaginary node that does not point to the original element. However, it prints out3. Why? - How come it prints out
1at the end? Thelistis not a circular queue.
Aucun commentaire:
Enregistrer un commentaire