Namespaces
Variants
Views
Actions

std::move_iterator

From cppreference.com
< cpp‎ | iterator
Revision as of 14:40, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <iterator>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template <class Iterator>
class move_iterator
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

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.

Contents

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&&

Member functions

Template:cpp/iterator/adaptor/dcl list constructorTemplate:cpp/iterator/adaptor/dcl list operator=Template:cpp/iterator/adaptor/dcl list baseTemplate:cpp/iterator/adaptor/dcl list operator*Template:cpp/iterator/adaptor/dcl list operator atTemplate:cpp/iterator/adaptor/dcl list operator arith

Non-member functions

Template:cpp/iterator/adaptor/dcl list operator cmpTemplate:cpp/iterator/adaptor/dcl list operator+Template:cpp/iterator/adaptor/dcl list operator-

Example

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <numeric>
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: "" "" "" ""

See also

Template:cpp/iterator/dcl list make move iterator