std::make_move_iterator
来自 cppreference.cn
定义于头文件 <iterator> |
||
template< class Iter > std::move_iterator<Iter> make_move_iterator( Iter i ); |
(自 C++11 起) (constexpr 自 C++17 起) |
|
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 = [](const auto rem, const auto& seq) { for (std::cout << rem; const auto& 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()); // copy std::vector<std::string> v2(std::make_move_iterator(s.begin()), std::make_move_iterator(s.end())); // move 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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2061 | C++11 | make_move_iterator 未将数组参数转换为指针 |
已修改为转换 |
[编辑] 参见
(C++11) |
解引用为右值的迭代器适配器 (类模板) |
(C++11) |
将参数转换为 xvalue (函数模板) |