名前空間
変種
操作

std::move

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
move
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダー <algorithm> で定義
template< class InputIt, class OutputIt >

OutputIt move( InputIt first, InputIt last,

               OutputIt d_first );
(1) (C++11以降)
(C++20 以降 constexpr)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt2 move( ExecutionPolicy&& policy,
                 ForwardIt1 first, ForwardIt1 last,

                 ForwardIt2 d_first );
(2) (C++17以降)
1) 範囲 [firstlast) の要素を、d_first から始まる別の範囲に移動します。移動操作の後、移動元の範囲の要素は適切な型の有効な値を保持しますが、移動前の値と同じである必要はありません。
2) (1) と同じですが、policy に従って実行されます。
このオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加します。

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true である。

(C++20まで)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true です。

(C++20以降)

d_first が範囲 [firstlast) 内にある場合、動作は未定義です。この場合、代わりに std::move_backward を使用できます。

目次

[編集] パラメーター

first, last - 移動する要素のソース範囲を定義するイテレータのペア
d_first - コピー先範囲の先頭
policy - 使用する 実行ポリシー
型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。

[編集] 戻り値

移動された最後の要素の次の要素へのイテレータ。

[編集] 計算量

正確に std::distance(first, last) 回のムーブ代入。

[編集] 例外

テンプレートパラメータ ExecutionPolicy を持つオーバーロードは、次のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外をスローし、ExecutionPolicy標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他の ExecutionPolicy の場合、動作は実装定義です。
  • アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。

[編集] 可能な実装

template<class InputIt, class OutputIt>
OutputIt move(InputIt first, InputIt last, OutputIt d_first)
{
    for (; first != last; ++d_first, ++first)
        *d_first = std::move(*first);
 
    return d_first;
}

[編集] 備考

範囲が重複している場合、左に移動するとき (目的範囲の先頭がソース範囲の外にあるとき) は std::move が適切であり、右に移動するとき (目的範囲の末尾がソース範囲の外にあるとき) は std::move_backward が適切です。

[編集]

以下のコードは、スレッドオブジェクト (それ自体はコピー不可能) をあるコンテナから別のコンテナに移動します。

#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <list>
#include <thread>
#include <vector>
 
void f(int n)
{
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "thread " << n << " ended" << std::endl;
}
 
int main()
{
    std::vector<std::jthread> v;
    v.emplace_back(f, 1);
    v.emplace_back(f, 2);
    v.emplace_back(f, 3);
    std::list<std::jthread> l;
 
    // copy() would not compile, because std::jthread is noncopyable
    std::move(v.begin(), v.end(), std::back_inserter(l));
}

出力

thread 1 ended
thread 2 ended
thread 3 ended

[編集] 関連項目

要素の範囲を逆順で新しい場所にムーブする
(関数テンプレート) [編集]
(C++11)
引数をxvalueに変換する
(関数テンプレート) [編集]
要素の範囲を新しい場所にムーブする
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)