< cpp‎ | iterator
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
Iterator adaptors
Stream iterators
Iterator customization points
Iterator operations
Range access
Defined in header <iterator>
template< class Iter >
class move_iterator;
(since C++11)

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.


[edit] Member types

Member type Definition
iterator_type Iter


(until C++20)

If std::iterator_traits<Iter>::iterator_category models std::derived_from<std::random_access_iterator_tag>, this is std::random_access_iterator_tag. Otherwise, this is std::iterator_traits<Iter>::iterator_category unchanged

(since C++20)
iterator_concept(C++20) std::input_iterator_tag


(until C++20)


(since C++20)


(until C++20)


(since C++20)
pointer Iter

If std::iterator_traits<Iter>::reference is a reference, this is the rvalue reference version of the same type. Otherwise (such as if the wrapped iterator returns by value), this is std::iterator_traits<Iter>::reference unchanged

(until C++20)


(since C++20)

[edit] Member functions

constructs a new iterator adaptor
(public member function) [edit]
assigns another iterator
(public member function) [edit]
accesses the underlying iterator
(public member function) [edit]
(deprecated in C++20)
accesses the pointed-to element
(public member function) [edit]
accesses an element by index
(public member function) [edit]
advances or decrements the iterator
(public member function) [edit]

[edit] Member objects

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

[edit] Non-member functions

compares the underlying iterators
(function template) [edit]
compares the underlying iterator and the underlying sentinel
(function template) [edit]
advances the iterator
(function template) [edit]
computes the distance between two iterator adaptors
(function template) [edit]
computes the distance between the underlying iterator and the underlying sentinel
(function template) [edit]
casts the result of dereferencing the underlying iterator to its associated rvalue reference type
(function template) [edit]
swap the objects pointed to by two underlying iterators
(function template) [edit]

[edit] Example

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <numeric>
#include <string>
int main()
    std::vector<std::string> v{"this", "is", "an", "example"};
    std::cout << "Old contents of the vector: ";
    for (auto& s : v)
        std::cout << '"' << s << "\" ";
    typedef std::vector<std::string>::iterator iter_t;
    std::string concat = std::accumulate(
                             std::string());  // Can be simplified with std::make_move_iterator
    std::cout << "\nConcatenated as string: " << concat << '\n'
              << "New contents of the vector: ";
    for (auto& s : v)
        std::cout << '"' << s << "\" ";
    std::cout << '\n';

Possible output:

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

[edit] Defect reports

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

DR Applied to Behavior as published Correct behavior
LWG 2106 C++11 dereferencing a move_iterator could return a dangling reference
if the dereferencing the underlying iterator returns a prvalue
returns the object instead

[edit] See also

creates a std::move_iterator of type inferred from the argument
(function template) [edit]
sentinel adaptor for use with std::move_iterator
(class template) [edit]