Based on these question: Is there a standard cyclic iterator in C++, Iterators for circular structures and Easiest way to make a cyclic iterator (circulator)? I assumed that there is no circular iterator in STL but then I found that the following code implements a circular like iterator, I did not find any documentation describing this behavior, so I decided to make sure about it here.
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> firstlist {1, 2, 3, 4, 5};
auto it = firstlist.begin();
for ( int i = 0; i < 20; i++ ) {
if(it == firstlist.end()) {
cout << "==end==" << endl;;
it++;
}
cout << i << ": " << *it++ << endl;
}
return 0;
}
The output:
0: 1
1: 2
2: 3
3: 4
4: 5
==end==
5: 1
6: 2
7: 3
8: 4
9: 5
==end==
10: 1
11: 2
12: 3
13: 4
14: 5
==end==
15: 1
16: 2
17: 3
18: 4
19: 5
Here is a link to execution: code