thrust::host_vector

Defined in thrust/host_vector.h

template<typename T, typename Alloc = std::allocator<T>>
class host_vector : public detail::vector_base<T, std::allocator<T>>

A host_vector is a container that supports random access to elements, constant time removal of elements at the end, and linear time insertion and removal of elements at the beginning or in the middle. The number of elements in a host_vector may vary dynamically; memory management is automatic. The memory associated with a host_vector resides in memory accessible to hosts.

See also

device_vector

See also

universal_vector

Public Functions

inline host_vector()

This constructor creates an empty host_vector.

inline host_vector(const Alloc &alloc)

This constructor creates an empty host_vector.

Parameters

alloc – The allocator to use by this host_vector.

inline ~host_vector()

The destructor erases the elements.

inline explicit host_vector(size_type n)

This constructor creates a host_vector with the given size.

Parameters

n – The number of elements to initially create.

inline host_vector(size_type n, default_init_t)

This constructor creates a host_vector with the given size, performing only default-initialization instead of value-initialization.

Parameters

n – The number of elements to initially create.

inline host_vector(size_type n, no_init_t)

This constructor creates a host_vector with the given size, without initializing elements. It mandates that the element type is trivially default-constructible.

Parameters

n – The number of elements to initially create.

inline explicit host_vector(size_type n, const Alloc &alloc)

This constructor creates a host_vector with the given size.

Parameters
  • n – The number of elements to initially create.

  • alloc – The allocator to use by this host_vector.

inline explicit host_vector(size_type n, const value_type &value)

This constructor creates a host_vector with copies of an exemplar element.

Parameters
  • n – The number of elements to initially create.

  • value – An element to copy.

inline explicit host_vector(size_type n, const value_type &value, const Alloc &alloc)

This constructor creates a host_vector with copies of an exemplar element.

Parameters
  • n – The number of elements to initially create.

  • value – An element to copy.

  • alloc – The allocator to use by this host_vector.

inline host_vector(const host_vector &v)

Copy constructor copies from an exemplar host_vector.

Parameters

v – The host_vector to copy.

inline host_vector(const host_vector &v, const Alloc &alloc)

Copy constructor copies from an exemplar host_vector.

Parameters
inline host_vector(host_vector &&v)

Move constructor moves from another host_vector.

Parameters

v – The host_vector to move.

inline host_vector(host_vector &&v, const Alloc &alloc)

Move constructor moves from another host_vector.

Parameters
inline host_vector &operator=(const host_vector &v)

Assign operator copies from an exemplar host_vector.

Parameters

v – The host_vector to copy.

inline host_vector &operator=(host_vector &&v)

Move assign operator moves from another host_vector.

Parameters

v – The host_vector to move.

template<typename OtherT, typename OtherAlloc>
inline host_vector(const host_vector<OtherT, OtherAlloc> &v)

Copy constructor copies from an exemplar host_vector with different type.

Parameters

v – The host_vector to copy.

template<typename OtherT, typename OtherAlloc>
inline host_vector &operator=(const host_vector<OtherT, OtherAlloc> &v)

Assign operator copies from an exemplar host_vector with different type.

Parameters

v – The host_vector to copy.

template<typename OtherT, typename OtherAlloc>
inline host_vector(const std::vector<OtherT, OtherAlloc> &v)

Copy constructor copies from an exemplar std::vector.

Parameters

v – The std::vector to copy.

template<typename OtherT, typename OtherAlloc>
inline host_vector &operator=(const std::vector<OtherT, OtherAlloc> &v)

Assign operator copies from an exemplar std::vector.

Parameters

v – The std::vector to copy.

template<typename OtherT, typename OtherAlloc>
inline host_vector(const detail::vector_base<OtherT, OtherAlloc> &v)

Copy construct from a vector_base whose element type is convertible to T.

Parameters

v – The vector_base to copy.

template<typename OtherT, typename OtherAlloc>
inline host_vector &operator=(const detail::vector_base<OtherT, OtherAlloc> &v)

Assign a vector_base whose element type is convertible to T.

Parameters

v – The vector_base to copy.

inline host_vector(::cuda::std::initializer_list<T> il)

This constructor builds a host_vector from an intializer_list.

Parameters

il – The intializer_list.

inline host_vector(::cuda::std::initializer_list<T> il, const Alloc &alloc)

This constructor builds a host_vector from an intializer_list.

Parameters
  • il – The intializer_list.

  • alloc – The allocator to use by this host_vector.

inline host_vector &operator=(::cuda::std::initializer_list<T> il)

Assign an intializer_list with a matching element type

Parameters

il – The intializer_list.

template<typename InputIterator>
inline host_vector(InputIterator first, InputIterator last)

This constructor builds a host_vector from a range.

Parameters
  • first – The beginning of the range.

  • last – The end of the range.

template<typename InputIterator>
inline host_vector(InputIterator first, InputIterator last, const Alloc &alloc)

This constructor builds a host_vector from a range.

Parameters
  • first – The beginning of the range.

  • last – The end of the range.

  • alloc – The allocator to use by this host_vector.

void resize(size_type new_size, const value_type &x = value_type())

Resizes this vector to the specified number of elements.

This method will resize this vector to the specified number of elements. If the number is smaller than this vector’s current size this vector is truncated, otherwise this vector is extended and new elements are populated with given data.

Parameters
  • new_size – Number of elements this vector should contain.

  • x – Data with which new elements should be populated.

Throws

std::length_error – If n exceeds max_size().

void resize(size_type new_size, default_init_t)

Resizes this vector to the specified number of elements, performing default-initialization instead of value-initialization.

Parameters

new_size – Number of elements this vector should contain.

Throws

std::length_error – If n exceeds max_size().

void resize(size_type new_size, no_init_t)

Resizes this vector to the specified number of elements, without initializing elements. It mandates that the element type is trivially default-constructible.

Parameters

new_size – Number of elements this vector should contain.

Throws

std::length_error – If n exceeds max_size().

size_type size() const

Returns the number of elements in this vector.

size_type max_size() const

Returns the size() of the largest possible vector.

Returns

The largest possible return value of size().

void reserve(size_type n)

If n is less than or equal to capacity(), this call has no effect. Otherwise, this method is a request for allocation of additional memory. If the request is successful, then capacity() is greater than or equal to n; otherwise, capacity() is unchanged. In either case, size() is unchanged.

Throws

std::length_error – If n exceeds max_size().

size_type capacity() const

Returns the number of elements which have been reserved in this vector.

void shrink_to_fit()

This method shrinks the capacity of this vector to exactly fit its elements.

reference operator[](size_type n)

Subscript access to the data contained in this vector_dev.

This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined.

Parameters

n – The index of the element for which data should be accessed.

Returns

Read/write reference to data.

const_reference operator[](size_type n) const

Subscript read access to the data contained in this vector_dev.

This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined.

Parameters

n – The index of the element for which data should be accessed.

Returns

Read reference to data.

iterator begin()

This method returns an iterator pointing to the beginning of this vector.

Returns

mStart

const_iterator begin() const

This method returns a const_iterator pointing to the beginning of this vector.

Returns

mStart

const_iterator cbegin() const

This method returns a const_iterator pointing to the beginning of this vector.

Returns

mStart

reverse_iterator rbegin()

This method returns a reverse_iterator pointing to the beginning of this vector’s reversed sequence.

Returns

A reverse_iterator pointing to the beginning of this vector’s reversed sequence.

const_reverse_iterator rbegin() const

This method returns a const_reverse_iterator pointing to the beginning of this vector’s reversed sequence.

Returns

A const_reverse_iterator pointing to the beginning of this vector’s reversed sequence.

const_reverse_iterator crbegin() const

This method returns a const_reverse_iterator pointing to the beginning of this vector’s reversed sequence.

Returns

A const_reverse_iterator pointing to the beginning of this vector’s reversed sequence.

iterator end()

This method returns an iterator pointing to one element past the last of this vector.

Returns

begin() + size().

const_iterator end() const

This method returns a const_iterator pointing to one element past the last of this vector.

Returns

begin() + size().

const_iterator cend() const

This method returns a const_iterator pointing to one element past the last of this vector.

Returns

begin() + size().

reverse_iterator rend()

This method returns a reverse_iterator pointing to one element past the last of this vector’s reversed sequence.

Returns

rbegin() + size().

const_reverse_iterator rend() const

This method returns a const_reverse_iterator pointing to one element past the last of this vector’s reversed sequence.

Returns

rbegin() + size().

const_reverse_iterator crend() const

This method returns a const_reverse_iterator pointing to one element past the last of this vector’s reversed sequence.

Returns

rbegin() + size().

const_reference front() const

This method returns a const_reference referring to the first element of this vector.

Returns

The first element of this vector.

reference front()

This method returns a reference pointing to the first element of this vector.

Returns

The first element of this vector.

const_reference back() const

This method returns a const reference pointing to the last element of this vector.

Returns

The last element of this vector.

reference back()

This method returns a reference referring to the last element of this vector_dev.

Returns

The last element of this vector.

pointer data()

This method returns a pointer to this vector’s first element.

Returns

A pointer to the first element of this vector.

const_pointer data() const

This method returns a const_pointer to this vector’s first element.

Returns

a const_pointer to the first element of this vector.

void clear()

This method resizes this vector to 0.

bool empty() const

This method returns true iff size() == 0.

Returns

true if size() == 0; false, otherwise.

void push_back(const value_type &x)

This method appends the given element to the end of this vector.

Parameters

x – The element to append.

void pop_back()

This method erases the last element of this vector, invalidating all iterators and references to it.

void swap(host_vector &v)

This method swaps the contents of this host_vector with another vector.

Parameters

v – The vector with which to swap.

iterator erase(iterator pos)

This method removes the element at position pos.

Parameters

pos – The position of the element of interest.

Returns

An iterator pointing to the new location of the element that followed the element at position pos.

iterator erase(iterator first, iterator last)

This method removes the range of elements [first,last) from this vector.

Parameters
  • first – The beginning of the range of elements to remove.

  • last – The end of the range of elements to remove.

Returns

An iterator pointing to the new location of the element that followed the last element in the sequence [first,last).

iterator insert(iterator position, const T &x)

This method inserts a single copy of a given exemplar value at the specified position in this vector.

Parameters
  • position – The insertion position.

  • x – The exemplar element to copy & insert.

Returns

An iterator pointing to the newly inserted element.

void insert(iterator position, size_type n, const T &x)

This method inserts a copy of an exemplar value to a range at the specified position in this vector.

Parameters
  • position – The insertion position

  • n – The number of insertions to perform.

  • x – The value to replicate and insert.

template<typename InputIterator>
void insert(iterator position, InputIterator first, InputIterator last)

This method inserts a copy of an input range at the specified position in this vector.

Parameters
  • position – The insertion position.

  • first – The beginning of the range to copy.

  • last – The end of the range to copy.

Template Parameters

InputIterator – is a model of <a href=”https://en.cppreference.com/w/cpp/iterator/input_iterator>Input Iterator, and InputIterator's value_type is a model of Assignable.

void assign(size_type n, const T &x)

This version of assign replicates a given exemplar n times into this vector.

Parameters
  • n – The number of times to copy x.

  • x – The exemplar element to replicate.

template<typename InputIterator>
void assign(InputIterator first, InputIterator last)

This version of assign makes this vector a copy of a given input range.

Parameters
  • first – The beginning of the range to copy.

  • last – The end of the range to copy.

Template Parameters

InputIterator – is a model of Input Iterator.

allocator_type get_allocator() const

This method returns a copy of this vector’s allocator.

Returns

A copy of the allocator used by this vector.

Friends

inline friend void swap(host_vector &a, host_vector &b) noexcept(noexcept(a.swap(b)))

Exchanges the values of two vectors. x The first host_vector of interest. y The second host_vector of interest.