Section

std::move_iterator

std::move_iterator is an iterator adaptor which behaves exactly like the underlying iterator (which must be at least a LegacyInputIteratoror model input_iterator(since C++20), or stronger iterator concept(since C++23)), 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.

# Declarations

template< class Iter >
class move_iterator;

(since C++11)

# Notes

Feature-test macro Value Std Feature __cpp_lib_move_iterator_concept 202207L (C++23) Make std::move_iterator<T*> a random access iterator

# Example

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
 
void print(const std::string_view rem, const auto& v)
{
    std::cout << rem;
    for (const auto& s : v)
        std::cout << std::quoted(s) << ' ';
    std::cout << '\n';
};
 
int main()
{
    std::vector<std::string> v{"this", "_", "is", "_", "an", "_", "example"};
    print("Old contents of the vector: ", v);
    std::string concat;
    for (auto begin = std::make_move_iterator(v.begin()),
              end = std::make_move_iterator(v.end());
         begin != end; ++begin)
    {
        std::string temp{*begin}; // moves the contents of *begin to temp
        concat += temp;
    }
 
    // Starting from C++17, which introduced class template argument deduction,
    // the constructor of std::move_iterator can be used directly:
    // std::string concat = std::accumulate(std::move_iterator(v.begin()),
    //                                      std::move_iterator(v.end()),
    //                                      std::string());
 
    print("New contents of the vector: ", v);
    print("Concatenated as string: ", std::ranges::single_view(concat));
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2106C++11dereferencing a move_iterator could return a dangling referenceif the dereferencing the underlying iterator returns a prvaluereturns theobject instead
LWG 3736C++20move_iterator was missing disable_sized_sentinel_for specializationadded
P2259R1C++20member iterator_category was defined even ifstd::iterator_traits::iterator_category is not definediterator_category isnot defined in this case

# See also