thrust::constant_iterator#
-
template<typename Value, typename Incrementable = use_default, typename System = use_default>
class constant_iterator : public thrust::iterator_adaptor<constant_iterator<Value, use_default, use_default>, base_iterator, Value, iterator_system_t<base_iterator>, iterator_traversal_t<base_iterator>, Value># constant_iteratoris an iterator which represents a pointer into a range of constant values.This iterator is useful for creating a range filled with the same value without explicitly storing it in memory. Using
constant_iteratorsaves both memory capacity and bandwidth.The following code snippet demonstrates how to create a
constant_iteratorwhosevalue_typeisintand whose value is10.#include <thrust/iterator/constant_iterator.h> thrust::constant_iterator<int> iter(10); *iter; // returns 10 iter[0]; // returns 10 iter[1]; // returns 10 iter[13]; // returns 10 // and so on...
This next example demonstrates how to use a
constant_iteratorwith thethrust::transformfunction to increment all elements of a sequence by the same value. We will create a temporaryconstant_iteratorwith the functionmake_constant_iteratorfunction in order to avoid explicitly specifying its type:#include <thrust/iterator/constant_iterator.h> #include <thrust/transform.h> #include <thrust/functional.h> #include <thrust/device_vector.h> int main() { thrust::device_vector<int> data{3, 7, 2, 5}; // add 10 to all values in data thrust::transform(data.begin(), data.end(), thrust::make_constant_iterator(10), data.begin(), ::cuda::std::plus<int>()); // data is now [13, 17, 12, 15] return 0; }
See also
Public Types
-
using base_type = Base#
The type of iterator this
iterator_adaptor'sadapts.
Public Functions
-
inline Base const &base() const#
- Returns:
A
constreference to theBaseiterator thisiterator_adaptoradapts.
-
using base_type = Base#