Get index from STL container iterator


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // distance example
  2. #include <iostream>
  3. #include <iterator>
  4. #include <list>
  5. using namespace std;
  6.  
  7. int main () {
  8. list<int> mylist;
  9. for (int i=0; i<10; i++) mylist.push_back (i*10);
  10.  
  11. list<int>::iterator first = mylist.begin();
  12. list<int>::iterator last = mylist.end();
  13.  
  14. cout << "The distance is: " << distance(first,last) << endl;
  15.  
  16. return 0;
  17. }

URL: http://www.cplusplus.com/reference/std/iterator/distance/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.