fork download
  1. #include <list>
  2. #include <string>
  3. #include <iostream>
  4. struct Point {
  5. std::string name;
  6. };
  7. int main()
  8. {
  9. std::list<Point> _pointList{{"foo"}, {"bar"}, {"baz"}};
  10. if(_pointList.size() > 1)
  11. {
  12. std::list<Point>::iterator itr;
  13. std::list<Point>::iterator itr2;
  14.  
  15. for(itr = _pointList.begin(); itr != _pointList.end(); itr++)
  16. {
  17. //set the second iterator to the next Point in the list.
  18. itr2 = itr;
  19. itr2++;
  20. if(itr2 == _pointList.end())
  21. itr2 = _pointList.begin();
  22.  
  23. std::cout << "The first point's name is: " << itr->name << '\n'
  24. << "The second point's name is: " << itr2->name << '\n';
  25. }
  26. }
  27. }
  28.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
The first point's name is: foo
The second point's name is: bar
The first point's name is: bar
The second point's name is: baz
The first point's name is: baz
The second point's name is: foo