在线文档教程
C++
迭代器 | Iterator

std::move_iterator

STD:移动[医]迭代器

Defined in header
template< class Iterator > class move_iterator;(since C++11)

std::move_iterator是迭代器适配器,它的行为与底层迭代器%28完全相同,至少必须是InputIterator%29,但反引用将基础迭代器返回的值转换为rvalue。如果将此迭代器用作输入迭代器,则其效果是将值移出,而不是从其复制。

成员类型

Member typeDefinition
iterator_typeIterator
difference_typestd::iterator_traits<Iterator>::difference_type
pointerIterator
value_typestd::iterator_traits<Iterator>::value_type
iterator_categorystd::iterator_traits<Iterator>::iterator_category
referencevalue_type&& (until C++17) If std::iterator_traits<Iterator>::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<Iterator>::reference unchanged. (since C++17)

成员函数

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

成员对象

Member nameDefinition
current (private)a copy of the base() iterator, the name is for exposition only

非会员职能

operator==operator!=operatoroperator>=compares the underlying iterators (function template)
operator+advances the iterator (function template)
operator-computes the distance between two iterator adaptors (function template)

二次

#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'; }

二次

可能的产出:

二次

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

二次

另见

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

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/iterator/move[医]迭代器