The Wayback Machine - https://web.archive.org/web/20180601205006/http://ja.cppreference.com:80/w/cpp/algorithm/move_backward
名前空間
変種
操作

std::move_backward

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
(C++11)
(C++11)
move_backward
(C++11)

未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
template<class BidirIt1, class BidirIt2 >
BidirIt2 move_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );
(C++11およびそれ以降)
(C++20以前)
template<class BidirIt1, class BidirIt2 >
constexpr BidirIt2 move_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );
(C++20およびそれ以降)

範囲 [first, last) の要素を d_last で終わる別の範囲にムーブします。 要素は逆順で (最後の要素が最初に) ムーブされますが、それらの相対順序は維持されます。

d_last(first, last] 内の場合、動作は未定義です。 その場合は std::move_backward の代わりに std::move を使用しなければなりません。

目次

[編集]引数

first, last - ムーブする要素の範囲
d_last - コピー先範囲の終端
型の要件
-
BidirIt1, BidirIt2BidirectionalIterator の要件を満たさなければなりません。

[編集]戻り値

コピー先範囲内の最後にムーブされた要素を指すイテレータ。

[編集]計算量

ちょうど last - first 回の代入。

[編集]実装例

template<class BidirIt1, class BidirIt2 > BidirIt2 move_backward(BidirIt1 first, BidirIt1 last, BidirIt2 d_last){while(first != last){*(--d_last)= std::move(*(--last));}return d_last;}

[編集]ノート

オーバーラップする範囲をムーブする場合、左にムーブする (ムーブ先範囲の先頭がムーブ元範囲の外側である) ときは std::move が適切であり、右にムーブする (ムーブ先範囲の終端がムーブ元範囲の外側である) ときは std::move_backward が適切です。

[編集]

#include <algorithm>#include <vector>#include <string>#include <iostream>   int main(){std::vector<std::string> src{"foo", "bar", "baz"};std::vector<std::string> dest(src.size());   std::cout<<"src: ";for(constauto&s : src){std::cout<< s <<' ';}std::cout<<"\ndest: ";for(constauto&s : dest){std::cout<< s <<' ';}std::cout<<'\n';   std::move_backward(src.begin(), src.end(), dest.end());   std::cout<<"src: ";for(constauto&s : src){std::cout<< s <<' ';}std::cout<<"\ndest: ";for(constauto&s : dest){std::cout<< s <<' ';}std::cout<<'\n';}

出力:

src: foo bar baz dest: src: dest: foo bar baz

[編集]関連項目

(C++11)
指定範囲の要素を新しい位置にムーブします
(関数テンプレート)[edit]
close