std::make_move_iterator
来自cppreference.com
在标头 <iterator> 定义 | ||
template<class Iter > std::move_iterator<Iter> make_move_iterator( Iter i ); | (C++11 起) (C++17 起为 constexpr ) | |
make_move_iterator
是便利函数模板,对给定迭代器 i 构造 std::move_iterator,其类型从实参类型推导。
目录 |
[编辑]参数
i | - | 转换成移动迭代器的输入迭代器 |
[编辑]返回值
std::move_iterator<Iter>(std::move(i))
[编辑]示例
运行此代码
#include <iomanip>#include <iostream>#include <iterator>#include <list>#include <string>#include <vector> auto print =[](constauto rem, constauto& seq){for(std::cout<< rem;constauto& 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 现在持有:", v1); print("v2 现在持有:", v2); print("原来的列表现在持有:", s);}
可能的输出:
v1 现在持有:"one" "two" "three" v2 现在持有:"one" "two" "three" 原来的列表现在持有:"" "" ""
[编辑]缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2061 | C++11 | make_move_iterator 不会将数组参数转换成指针 | 会转换 |
[编辑]参阅
(C++11) | 解引用结果为右值的迭代器适配器 (类模板) |
(C++11) | 转换实参为亡值 (函数模板) |