cpp/iterator/move iterator: Difference between revisions
From cppreference.com
D41D8CD98F (talk | contribs) |
→Non-member functions: C++20 uilities |
||
| Line 41: | Line 41: | ||
{{dsc begin}} | {{dsc begin}} | ||
{{dsc inc | cpp/iterator/adaptor/dsc operator_cmp | move_iterator}} | {{dsc inc | cpp/iterator/adaptor/dsc operator_cmp | move_iterator}} | ||
{{dsc inc | cpp/iterator/adaptor/dsc operator+ | move_iterator}} | {{dsc inc | cpp/iterator/adaptor/dsc operator+ | move_iterator}} | ||
{{dsc inc | cpp/iterator/adaptor/dsc operator- | move_iterator}} | {{dsc inc | cpp/iterator/adaptor/dsc operator- | move_iterator}} | ||
{{dsc end}} | {{dsc end}} | ||
Revision as of 05:09, 14 June 2019
| 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), 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
| ||||
difference_type
|
std::iterator_traits<Iter>::difference_type
| ||||
pointer
|
Iter
| ||||
value_type
|
std::iterator_traits<Iter>::value_type
| ||||
iterator_category
|
std::iterator_traits<Iter>::iterator_category
| ||||
reference
|
|
Member functions
constructs a new move_iterator (public member function) | |
assigns another move_iterator (public member function) | |
| accesses the underlying iterator (public member function) | |
| accesses the pointed-to element (public member function) | |
| accesses an element by index (public member function) | |
advances or decrements the move_iterator (public member function) |
Member objects
| Member name | Definition |
current (private)
|
a copy of the base() iterator, the name is for exposition only |
Non-member functions
(C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20) |
compares the underlying iterators (function template) |
| compares the underlying iterator and the underlying sentinel (function template) | |
(C++11) |
advances the iterator (function template) |
(C++11) |
computes the distance between two iterator adaptors (function template) |
| computes the distance between the underlying iterator and the underlying sentinel (function template) | |
(C++20) |
casts the result of dereferencing the underlying iterator to its associated rvalue reference type (function) |
(C++20) |
swaps the objects pointed to by two underlying iterators (function template) |
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';
}
Possible output:
Old contents of the vector: "this" "is" "an" "example"
Concatenated as string: thisisanexample
New contents of the vector: "" "" "" ""
See also
(C++11) |
creates a std::move_iterator of type inferred from the argument (function template) |