#include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; int main() { list<int> coll; for (int i = 1; i <= 9; ++i) { coll.push_back(i); } list<int>::iterator pos = coll.begin(); cout << *pos << endl; advance(pos, 3); cout << *pos << endl; advance(pos, -1); cout << *pos << endl; int size = distance(coll.begin(), coll.end()); printf("size is %d\n", size); getchar(); return 0; }
運行結果:html
參考:http://www.programlife.net/stl-iterator-funcation-advance.htmlios
http://www.programlife.net/stl-iterator-funcation-distance.htmlspa