Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
Iterators allow a uniform way to travel through all STL containers.
Screen output:
Note the following about the code shown above:
- The preferred way to display the contents of the list can be found at the CoutContainer code snippet. Prefer using standard algorithms, instead of crafting your own for loops [1].
- std::list<int>::const_iterator was used, instead of std::list<int>::iterator. Use const_iterator when you are not modifying the contents of a container [2].
- operator!= was used instead of operator<. Prefer comparing iterators with operator!=, instead of operator< [3].
- The prefix form of operator++ was used (that is '++i' instead of 'i++'). Prefer using the prefix form, unless you really need the old value [4].
- Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 1 guideline: 'Prefer reusing algorithms, particularly standard algorithms (e.g., for_each), instead of crafting your own loops'.
- Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 1 guideline: 'Use const_iterator when you are not modifying the contents of a container'.
- Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 1 guideline: 'Prefer comparing iterators with !=, not <'.
- Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 1 guideline: 'Get in the habit of using the prefix forms of -- and ++ by default, unless you really need the old value'.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
