std::reverse_copy
提供: cppreference.com
ヘッダ <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 | - | コピー先範囲の先頭 |
型の要件 | ||
-BidirIt は BidirectionalIterator の要件を満たさなければなりません。 | ||
-OutputIt は OutputIterator の要件を満たさなければなりません。 | ||
-ForwardIt は ForwardIterator の要件を満たさなければなりません。 |
[編集]戻り値
最後にコピーした要素の次の要素を指す出力イテレータ。
[編集]例外
テンプレート引数 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;} |
[編集]例
Run this code
#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
[編集]計算量
first
と last
の距離に比例。
[編集]関連項目
指定範囲の要素の順序を反転させます (関数テンプレート) |