Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Forward and Bidirectional Iterators | Iterator Categories
C++ STL Iterators

bookForward and Bidirectional Iterators

Swipe um das Menü anzuzeigen

Note
Definition

A forward iterator is an iterator that supports multi-pass traversal, meaning you can traverse the same sequence multiple times using different iterators, and it allows only the increment (++) operation to move forward. In contrast, a bidirectional iterator also supports multi-pass traversal but allows both increment (++) and decrement (--) operations, enabling movement in both directions through a sequence.

Forward and bidirectional iterators are essential in the C++ Standard Template Library (STL) for traversing containers with varying capabilities. Both iterator types guarantee multi-pass traversal, so you can have several iterators moving independently over the same container without invalidating each other, as long as the container is not modified. The key distinction is in the operations they support: forward iterators can only move forward using the ++ operator, while bidirectional iterators can move both forward (++) and backward (--).

Containers that provide forward iterators include std::forward_list, which is a singly-linked list optimized for one-way traversal. Bidirectional iterators are provided by containers like std::list, std::set, std::map, and others, allowing you to traverse in both directions.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132
#include <iostream> #include <forward_list> #include <list> int main() { // Traversing std::forward_list with a forward iterator std::forward_list<int> flist = {1, 2, 3, 4}; std::cout << "std::forward_list traversal (forward only): "; for (auto it = flist.begin(); it != flist.end(); ++it) std::cout << *it << " "; std::cout << std::endl; // Traversing std::list with a bidirectional iterator std::list<int> dlist = {10, 20, 30, 40}; std::cout << "std::list forward traversal: "; for (auto it = dlist.begin(); it != dlist.end(); ++it) std::cout << *it << " "; std::cout << std::endl; std::cout << "std::list backward traversal: "; auto it = dlist.end(); do { --it; std::cout << *it << " "; } while (it != dlist.begin()); std::cout << std::endl; }

In the code above, you see two STL containers in action: std::forward_list and std::list. The std::forward_list is traversed using a forward iterator, which only supports incrementing with ++. You cannot use -- to move backward. The std::list, on the other hand, is traversed forward using ++ and then backward using --, thanks to its bidirectional iterator. Notice how the backward traversal starts from end() and decrements the iterator until it reaches begin(). This demonstrates the flexibility of bidirectional iterators compared to forward iterators.

main.cpp

main.cpp

copy
12345678910
#include <forward_list> int main() { std::forward_list<int> flist = {1, 2, 3}; auto it = flist.begin(); // Uncommenting the following line will cause a compilation error: // --it; return 0; }

The previous example attempts to use the decrement operator (--) on a forward iterator from std::forward_list. This will result in a compilation error, because forward iterators do not support the -- operation. Only containers with bidirectional iterators, such as std::list, allow both increment and decrement.

Iterator Capabilities
expand arrow
  • Forward iterators support only forward movement using ++.
  • Applying the decrement operator (--) to a forward iterator (such as from std::forward_list) results in a compilation error.
  • Only containers with bidirectional iterators, like std::list, support both ++ and --.
Container Characteristics
expand arrow
  • std::forward_list is optimized for one-direction traversal and lower memory usage.
  • std::list supports traversal in both directions but comes with additional overhead.
Choosing the Right Container
expand arrow
  • Use std::forward_list when only forward traversal is required.
  • Use a container with bidirectional iterators when backward traversal is necessary.
question mark

Which operation is NOT supported by forward iterators?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 2. Kapitel 2
some-alt