Iterators#

Thrust provides a rich collection of iterators that extend beyond the C++ standard library.

Important

Many Thrust iterators have direct replacements in libcu++, which are newer and thus fix some of the shortcomings of Thrust’s iterators. They should generally be preferred. See Fancy Iterators for more.

Fancy iterators#

Thrust provides the following fancy iterators:

Iterator traits#

Thrust offers the following traits to inspect iterators:

Iterator tags#

Thrust provides the following iterator category tags:

Additionally, the following utilities for working with iterator category tags are provided:

Thrust provides the following iterator traversal tags:

Iterator model#

Thrust’s iterators extend the C++ standard library iterator model and were inspired by Boost.Iterator. In the C++ standard library, iterators are mostly distinguished by their iterator category (before C++20), and their iterator concept (since C++20). This is governed by the nested iterator_category and iterator_concept types, respectively, which in standard C++ are one of:

  • input_iterator_tag

  • output_iterator_tag

  • forward_iterator_tag

  • bidirectional_iterator_tag

  • random_access_iterator_tag

  • contiguous_iterator_tag (since C++20)

Thrust extends this model by introducing the notion of an iterator traversal and an iterator system. In order to fit into the existing schema of iterators, it encodes this additional information into the iterator_category, which is no longer one of the standard tags listed above, but a templated type carrying the iterator tag, iterator traversal and iterator system. This information can be extracted using the thrust::iterator_traversal and thrust::iterator_system traits.