std::make_move_iterator
来自cppreference.com
在标头 <iterator> 定义
|
||
template< class Iter > std::move_iterator<Iter> make_move_iterator( Iter i ); |
(C++11 起) (C++17 前) |
|
template< class Iter > constexpr std::move_iterator<Iter> make_move_iterator( Iter i ); |
(C++17 起) | |
make_move_iterator
是便利函数模板,对给定迭代器 i 构造 std::move_iterator,其类型从实参类型推导。
参数
i | - | 转换成移动迭代器的输入迭代器 |
返回值
可用于从通过 i 访问的元素移动的 std::move_iterator。
可能的实现
template<class Iter> constexpr std::move_iterator<Iter> make_move_iterator(Iter i) { return std::move_iterator<Iter>(std::move(i)); } |
示例
运行此代码
#include <iomanip> #include <iostream> #include <iterator> #include <list> #include <string> #include <vector> auto print = [](auto const rem, auto const& seq) { for (std::cout << rem; auto const& str : seq) std::cout << std::quoted(str) << ' '; std::cout << '\n'; }; int main() { std::list<std::string> s{"one", "two", "three"}; std::vector<std::string> v1(s.begin(), s.end()); // 复制 std::vector<std::string> v2(std::make_move_iterator(s.begin()), std::make_move_iterator(s.end())); // 移动 print("v1 now holds: ", v1); print("v2 now holds: ", v2); print("original list now holds: ", s); }
可能的输出:
v1 now holds: "one" "two" "three" v2 now holds: "one" "two" "three" original list now holds: "" "" ""
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2061 | C++11 | make_move_iterator 不将数组参数转换成指针
|
使之转换 |
参阅
(C++11) |
解引用结果为右值的迭代器适配器 (类模板) |
(C++11) |
获得右值引用 (函数模板) |