thrust::reverse_iterator
Defined in thrust/iterator/reverse_iterator.h
- 
template<typename BidirectionalIterator>
 class reverse_iterator
- reverse_iteratoris an iterator which represents a pointer into a reversed view of a given range. In this way,- reverse_iteratorallows backwards iteration through a bidirectional input range.- It is important to note that although - reverse_iteratoris constructed from a given iterator, it points to the element preceding it. In this way, the past-the-end- reverse_iteratorof a given range points to the element preceding the first element of the input range. By the same token, the first- reverse_iteratorof a given range is constructed from a past-the-end iterator of the original range yet points to the last element of the input.- The following code snippet demonstrates how to create a - reverse_iteratorwhich represents a reversed view of the contents of a- device_vector.- #include <thrust/iterator/reverse_iterator.h> #include <thrust/device_vector.h> ... thrust::device_vector<float> v(4); v[0] = 0.0f; v[1] = 1.0f; v[2] = 2.0f; v[3] = 3.0f; using Iterator = thrust::device_vector<float>::iterator; // note that we point the iterator to the *end* of the device_vector thrust::reverse_iterator<Iterator> iter(values.end()); *iter; // returns 3.0f; iter[0]; // returns 3.0f; iter[1]; // returns 2.0f; iter[2]; // returns 1.0f; iter[3]; // returns 0.0f; // iter[4] is an out-of-bounds error - Since reversing a range is a common operation, containers like - device_vectorhave nested aliases for declaration shorthand and methods for constructing reverse_iterators. The following code snippet is equivalent to the previous:- #include <thrust/device_vector.h> ... thrust::device_vector<float> v(4); v[0] = 0.0f; v[1] = 1.0f; v[2] = 2.0f; v[3] = 3.0f; // we use the nested type reverse_iterator to refer to a reversed view of a device_vector and the method rbegin() to // create a reverse_iterator pointing to the beginning of the reversed device_vector thrust::device_iterator<float>::reverse_iterator iter = values.rbegin(); *iter; // returns 3.0f; iter[0]; // returns 3.0f; iter[1]; // returns 2.0f; iter[2]; // returns 1.0f; iter[3]; // returns 0.0f; // iter[4] is an out-of-bounds error // similarly, rend() points to the end of the reversed sequence: assert(values.rend() == (iter + 4)); - Finally, the following code snippet demonstrates how to use reverse_iterator to perform a reversed prefix sum operation on the contents of a device_vector: - #include <thrust/device_vector.h> #include <thrust/scan.h> ... thrust::device_vector<int> v(5); v[0] = 0; v[1] = 1; v[2] = 2; v[3] = 3; v[4] = 4; thrust::device_vector<int> result(5); // exclusive scan v into result in reverse thrust::exclusive_scan(v.rbegin(), v.rend(), result.begin()); // result is now {0, 4, 7, 9, 10} - See also - make_reverse_iterator - Public Functions - 
reverse_iterator() = default
 - 
inline explicit reverse_iterator(BidirectionalIterator x)
- Constructoraccepts a- BidirectionalIteratorpointing to a range for this- reverse_iteratorto reverse.- Parameters
- x – A - BidirectionalIteratorpointing to a range to reverse.
 
 - 
template<typename OtherBidirectionalIterator, detail::enable_if_convertible_t<OtherBidirectionalIterator, BidirectionalIterator, int> = 0>
 inline reverse_iterator(reverse_iterator<OtherBidirectionalIterator> const &rhs)
- Copyconstructor allows construction from a related compatible- reverse_iterator.- Parameters
- r – A - reverse_iteratorto copy from.
 
 
- 
reverse_iterator() = default