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

std::reverse_copy

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template<class BidirIt, class OutputIt >
OutputIt reverse_copy( BidirIt first, BidirIt last, OutputIt d_first );
(C++20以前)
template<class BidirIt, class OutputIt >
constexpr OutputIt reverse_copy( BidirIt first, BidirIt last, OutputIt d_first );
(C++20およびそれ以降)
template<class ExecutionPolicy, class BidirIt, class ForwardIt >
ForwardIt reverse_copy( ExecutionPolicy&& policy, BidirIt first, BidirIt last, ForwardIt d_first );
(2) (C++17およびそれ以降)
1) 範囲 [first, last) の要素を d_first で始まる別の範囲に反転した順序になるようにコピーします。
非負の i < (last - first) のそれぞれについて一度ずつ代入 *(d_first +(last - first)-1- i)=*(first + i) を実行したかのように動作します。
コピー元とコピー先の範囲 (つまり、 [first, last)[d_first, d_first+(last-first))) がオーバーラップしている場合、動作は未定義です。
2)(1) と同じですが、 policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。

目次

[編集]引数

first, last - コピーする要素の範囲
d_first - コピー先範囲の先頭
型の要件
-
BidirItBidirectionalIterator の要件を満たさなければなりません。
-
OutputItOutputIterator の要件を満たさなければなりません。
-
ForwardItForwardIterator の要件を満たさなければなりません。

[編集]戻り値

最後にコピーした要素の次の要素を指す出力イテレータ。

[編集]例外

テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外を投げ、 ExecutionPolicy が3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆる ExecutionPolicy については、動作は処理系定義です。
  • アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。

[編集]実装例

template<class BidirIt, class OutputIt> OutputIt reverse_copy(BidirIt first, BidirIt last, OutputIt d_first){while(first != last){*(d_first++)=*(--last);}return d_first;}

[編集]

#include <vector>#include <iostream>#include <algorithm>   int main(){std::vector<int> v({1,2,3});for(constauto& value : v){std::cout<< value <<" ";}std::cout<<'\n';   std::vector<int> destination(3); std::reverse_copy(std::begin(v), std::end(v), std::begin(destination));for(constauto& value : destination){std::cout<< value <<" ";}std::cout<<'\n';}

出力:

1 2 3 3 2 1

[編集]計算量

firstlast の距離に比例。

[編集]関連項目

指定範囲の要素の順序を反転させます
(関数テンプレート)[edit]
close