std::copy, std::copy_if
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <algorithm> で定義
|
||
| (1) | ||
template< class InputIt, class OutputIt > OutputIt copy( InputIt first, InputIt last, OutputIt d_first ); |
(C++20未満) | |
template< class InputIt, class OutputIt > constexpr OutputIt copy( InputIt first, InputIt last, OutputIt d_first ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt2 copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first ); |
(2) | (C++17以上) |
| (3) | ||
template< class InputIt, class OutputIt, class UnaryPredicate > OutputIt copy_if( InputIt first, InputIt last, OutputIt d_first, UnaryPredicate pred ); |
(C++11以上) (C++20未満) |
|
template< class InputIt, class OutputIt, class UnaryPredicate > constexpr OutputIt copy_if( InputIt first, InputIt last, OutputIt d_first, UnaryPredicate pred ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate > ForwardIt2 copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, UnaryPredicate pred ); |
(4) | (C++17以上) |
[first, last) で定義される範囲内の要素を d_first から始まる別の範囲にコピーします。
1) 範囲
[first, last) の要素をすべて、 first から開始して last - 1 まで、コピーします。 d_first が範囲 [first, last) 内の場合、動作は未定義です。 その場合、代わりに std::copy_backward が使用できます。3) 述語
pred が true を返す要素のみをコピーします。 コピーされる要素の相対順序は維持されます。 コピー元とコピー先の範囲がオーバーラップしている場合、動作は未定義です。2,4) (1,3) と同じですが、
policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します引数
| first, last | - | コピーする要素の範囲 |
| d_first | - | コピー先の範囲の先頭 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| pred | - | 要求された要素に対して true を返す単項述語。 式 |
| 型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。
| ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-UnaryPredicate は Predicate の要件を満たさなければなりません。
| ||
戻り値
コピー先の範囲内の最後にコピーした要素の次の要素を指す出力イテレータ。
計算量
- 1-2) ちょうど
(last - first)回の代入。
- 3-4) ちょうど
(last - first)回の述語の適用および0〜(last - first)回の代入 (述語と入力データに依存し、述語が true を返すすべての要素について、代入が行われます)。
ExecutionPolicy を取るオーバーロードの場合、 ForwardIt1 の valie_type が MoveConstructible でなければ、性能上のコストが生じることがあります。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
ノート
実際のところ、 std::copy の実装は、値の型が TriviallyCopyable であれば、複数回の代入を避け、 std::memmove のようなバルクコピー関数を使用します。
オーバーラップする範囲をコピーする場合、左にコピーする (コピー先範囲の先頭がコピー元範囲の外側である) ときは std::copy が適切であり、右にコピーする (コピー先範囲の終端がコピー元範囲の外側である) ときは std::copy_backward が適切です。
実装例
| 1つめのバージョン |
|---|
template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last,
OutputIt d_first)
{
while (first != last) {
*d_first++ = *first++;
}
return d_first;
}
|
| 2つめのバージョン |
template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last,
OutputIt d_first, UnaryPredicate pred)
{
while (first != last) {
if (pred(*first))
*d_first++ = *first;
first++;
}
return d_first;
}
|
例
以下のコードは、あるベクタから別のベクタへの内容のコピーと、その結果のベクタの表示の、両方に copy を使用します。
Run this code
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
#include <numeric>
int main()
{
std::vector<int> from_vector(10);
std::iota(from_vector.begin(), from_vector.end(), 0);
std::vector<int> to_vector;
std::copy(from_vector.begin(), from_vector.end(),
std::back_inserter(to_vector));
// or, alternatively,
// std::vector<int> to_vector(from_vector.size());
// std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());
// either way is equivalent to
// std::vector<int> to_vector = from_vector;
std::cout << "to_vector contains: ";
std::copy(to_vector.begin(), to_vector.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
出力:
to_vector contains: 0 1 2 3 4 5 6 7 8 9
関連項目
| 指定範囲の要素を後ろからコピーします (関数テンプレート) | |
| 指定範囲の要素の順序を反転させたコピーを作成します (関数テンプレート) | |
(C++11) |
指定個数の要素を新しい位置にコピーします (関数テンプレート) |
| 指定された要素を範囲内の全要素にコピー代入します (関数テンプレート) | |
| 指定範囲の要素から一定の基準を満たすものを除いてコピーします (関数テンプレート) |