Back to Basics: Iterators in C++ - Nicolai Josuttis - CppCon 2023

C++ iterators allow you to iterate over elements in a container or range, providing a way to access and manipulate data independently of its underlying type.

Key takeaways
  • Iterators are a fundamental concept in C++ that allow you to iterate over elements in a container.
  • Iterators can be categorized into different types, including input iterators, output iterators, forward iterators, bidirectional iterators, and random access iterators.
  • The iterator categories have changed in C++20, with the introduction of a new category called “sentinel” iterators.
  • Iterators can be used to iterate over elements in a container, and can be used with algorithms to perform operations on the elements.
  • Iterators can also be used to iterate over elements in a range, which is a sequence of elements that can be iterated over.
  • The begin() and end() functions are used to get the first and last iterators of a range, respectively.
  • The ++ operator is used to increment an iterator to the next element in the range.
  • The -- operator is used to decrement an iterator to the previous element in the range.
  • The * operator is used to dereference an iterator and access the element it points to.
  • The -> operator is used to access the elements of a container using an iterator.
  • Iterators can be used to iterate over elements in a container in a generic way, without knowing the specific type of the container.
  • Iterators can be used to iterate over elements in a range in a generic way, without knowing the specific type of the range.
  • The auto keyword can be used to automatically deduce the type of an iterator.
  • The const keyword can be used to specify that an iterator should not be modified.
  • The iterator_category type trait can be used to determine the category of an iterator.
  • The iterator_traits type trait can be used to determine the properties of an iterator.
  • Iterators can be used to iterate over elements in a container in a way that is independent of the specific type of the container.
  • Iterators can be used to iterate over elements in a range in a way that is independent of the specific type of the range.
  • The std::begin() and std::end() functions can be used to get the first and last iterators of a range, respectively.
  • The std::advance() function can be used to advance an iterator to a specific position in the range.
  • The std::distance() function can be used to get the distance between two iterators in a range.
  • The std::next() function can be used to get the next iterator in a range.
  • The std::prev() function can be used to get the previous iterator in a range.
  • The std::advance() function can be used to advance an iterator to a specific position in the range.
  • The std::distance() function can be used to get the distance between two iterators in a range.
  • The std::next() function can be used to get the next iterator in a range.
  • The std::prev() function can be used to get the previous iterator in a range.