命名空间
变体
操作

std::make_move_iterator

来自 cppreference.cn
< cpp‎ | iterator
 
 
迭代器库
迭代器概念
迭代器原语
算法概念和工具
间接可调用概念
通用算法要求
(C++20)
(C++20)
(C++20)
实用工具
(C++20)
迭代器适配器
范围访问
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
定义于头文件 <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)
将参数转换为 xvalue
(函数模板) [编辑]