std::reverse_copy
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <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 は LegacyBidirectionalIterator の要件を満たさなければなりません。
| ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。
| ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。
| ||
戻り値
最後にコピーした要素の次の要素を指す出力イテレータ。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
実装例
libstdc++ と libc++ の実装も参照してください。
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 (const auto& 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 (const auto& value : destination) {
std::cout << value << " ";
}
std::cout << '\n';
}
出力:
1 2 3
3 2 1
計算量
first と last の距離に比例。
関連項目
| 指定範囲の要素の順序を反転させます (関数テンプレート) |