CppDS.com

C++ 98 11 14 17 20 手册

std::make_move_iterator

来自cppreference.com
< cpp‎ | iterator
 
 
迭代器库
迭代器概念
迭代器原语
算法概念与工具
间接可调用概念
常用算法要求
工具
迭代器适配器
流迭代器
迭代器定制点
迭代器操作
(C++11)
(C++11)
范围访问
(C++11)(C++14)
(C++11)(C++14)
(C++17)(C++20)
(C++14)(C++14)
(C++14)(C++14)
(C++17)
(C++17)
 
定义于头文件 <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 // C++17 起
std::move_iterator<Iter> make_move_iterator( Iter i )
{
    return std::move_iterator<Iter>(std::move(i));
}

示例

#include <iostream>
#include <list>
#include <vector>
#include <string>
#include <iterator>
 
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
 
    std::cout << "v1 now holds: ";
    for (auto str : v1)
            std::cout << "\"" << str << "\" ";
    std::cout << "\nv2 now holds: ";
    for (auto str : v2)
            std::cout << "\"" << str << "\" ";
    std::cout << "\noriginal list now holds: ";
    for (auto str : s)
            std::cout << "\"" << str << "\" ";
    std::cout << '\n';
}

可能的输出:

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)
获得右值引用
(函数模板)
关闭