std::move_iterator - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::move_iterator

From cppreference.com
< cpp‎ | iterator

Defined in header `<iterator>` |  |
template< class Iterator > 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 `InputIterator`), 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` | `Iterator`
`difference_type` | std::iterator_traits<Iterator>::difference_type
`pointer` | `Iterator`
`value_type` | std::iterator_traits<Iterator>::value_type
`iterator_category` | std::iterator_traits<Iterator>::iterator_category
`reference` | `value_type&&`

### [edit] Member functions

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

### [edit] Non-member functions

operator==operator!=operator<operator<=operator>operator>= | compares the underlying iterators (function template) [edit]
operator+ | advances the iterator (function template) [edit]
operator- | computes the distance between two iterator adaptors (function template) [edit]

### [edit] Example

Run this code

    #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::move_iterator<iter_t>(v.begin()),
                                 std::move_iterator<iter_t>(v.end()),
                                 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';
    }

Output:

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

### [edit] See also

make_move_iterator (C++11) | creates a std::move_iterator of type inferred from the argument (function template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/move_iterator&oldid=69160"