Scroll to navigation

std::move_iterator(3) C++ Standard Libary std::move_iterator(3)

NAME

std::move_iterator - std::move_iterator

Synopsis


Defined in header <iterator>
template< class Iter > (since C++11)
class move_iterator;


std::move_iterator is an iterator adaptor which behaves exactly like the underlying
iterator (which must be at least an LegacyInputIterator
or model input_iterator
(since C++20)), except that dereferencing converts the value returned by the
underlying iterator into an rvalue. If this iterator is used as an input iterator,
the effect is that the values are moved from, rather than copied from.

Member types


Member type Definition
iterator_type Iter
std::iterator_traits<Iter>::iterator_category (until
C++20)
If std::iterator_traits<Iter>::iterator_category is
valid and denotes a type:


* if std::iterator_traits<Iter>::iterator_category
models
iterator_category std::derived_from<std::random_access_iterator_tag>, (since
member iterator_category is C++20)
std::random_access_iterator_tag.
* Otherwise, member iterator_category is the same
type as
std::iterator_traits<Iter>::iterator_category.


Otherwise, there is no member iterator_category.
iterator_concept(C++20) std::input_iterator_tag
value_type std::iterator_traits<Iter>::value_type (until C++20)
std::iter_value_t<Iter> (since C++20)
difference_type std::iterator_traits<Iter>::difference_type (until C++20)
std::iter_difference_t<Iter> (since C++20)
pointer Iter
If std::iterator_traits<Iter>::reference is a
reference, this is the rvalue reference version of the (until
same type. Otherwise (such as if the wrapped iterator C++20)
reference returns by value), this is
std::iterator_traits<Iter>::reference unchanged
std::iter_rvalue_reference_t<Iter> (since
C++20)

Member functions


constructor constructs a new iterator adaptor
(C++11) (public member function)
operator= assigns another iterator adaptor
(C++11) (public member function)
base accesses the underlying iterator
(C++11) (public member function)
operator*
operator-> accesses the pointed-to element
(C++11) (public member function)
(C++11)(deprecated in C++20)
operator[] accesses an element by index
(C++11) (public member function)
operator++
operator++(int)
operator+=
operator+ advances or decrements the iterator
operator-- (public member function)
operator--(int)
operator-=
operator-
(C++11)

Member objects


Member name Definition
the underlying iterator from which base() copies
current (private) or moves
(since C++20), the name is for exposition only

Non-member functions


operator==
operator!=
operator<
operator<=
operator>
operator>=
operator<=> compares the underlying iterators
(C++11) (function template)
(C++11)(removed in C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++20)
operator==(std::move_sentinel) compares the underlying iterator and the underlying
(C++20) sentinel
(function template)
operator+ advances the iterator
(C++11) (function template)
operator- computes the distance between two iterator adaptors
(C++11) (function template)
operator-(std::move_sentinel) computes the distance between the underlying iterator
(C++20) and the underlying sentinel
(function template)
iter_move casts the result of dereferencing the underlying
(C++20) iterator to its associated rvalue reference type
(function)
iter_swap swaps the objects pointed to by two underlying
(C++20) iterators
(function template)
make_move_iterator creates a std::move_iterator of type inferred from
(C++11) the argument
(function template)

Example

// Run this code


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <iterator>
#include <numeric>
#include <string>


int main()
{
std::vector<std::string> v{"this", "_", "is", "_", "an", "_", "example"};


auto print_v = [&](auto const rem) {
std::cout << rem;
for (const auto& s : v)
std::cout << std::quoted(s) << ' ';
std::cout << '\n';
};


print_v("Old contents of the vector: ");


std::string concat = std::accumulate(std::make_move_iterator(v.begin()),
std::make_move_iterator(v.end()),
std::string());


/* An alternative that uses std::move_iterator directly could be:
using moviter_t = std::move_iterator<std::vector<std::string>::iterator>;
std::string concat = std::accumulate(moviter_t(v.begin()),
moviter_t(v.end()),
std::string()); */


print_v("New contents of the vector: ");


std::cout << "Concatenated as string: " << quoted(concat) << '\n';
}

Possible output:


Old contents of the vector: "this" "_" "is" "_" "an" "_" "example"
New contents of the vector: "" "" "" "" "" "" ""
Concatenated as string: "this_is_an_example"


Defect reports


The following behavior-changing defect reports were applied retroactively to
previously published C++ standards.


DR Applied Behavior as Correct behavior
to published
dereferencing a
move_iterator
could return a
dangling reference
LWG 2106 C++11 if the returns the object instead
dereferencing the
underlying
iterator returns a
prvalue
member defined only if
P2259R1 C++20 iterator_category std::iterator_traits<Iter>::iterator_category
is always defined exists

See also


make_move_iterator creates a std::move_iterator of type inferred from the argument
(C++11) (function template)
move_sentinel sentinel adaptor for use with std::move_iterator
(C++20) (class template)

2022.07.31 http://cppreference.com