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

std::copy, std::copy_if

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
copycopy_if
(C++11)
(C++11)
(C++11)

未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲に対する)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
template<class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );
(1)
template<class InputIt, class OutputIt, class UnaryPredicate >

OutputIt copy_if( InputIt first, InputIt last,
                  OutputIt d_first,

                  UnaryPredicate pred );
(2) (C++11およびそれ以降)
[first, last)で別の範囲の先頭に、d_firstによって定義され、範囲内の要素をコピーします。第二の機能は、要素だけをコピーしているの述語pred戻りtrue.
Original:
Copies the elements in the range, defined by [first, last), to another range beginning at d_first. The second function only copies the elements for which the predicate pred returns true.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集]パラメータ

first, last -
コピーする要素の範囲
Original:
the range of elements to copy
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
d_first -
目的の範囲の始まり。 d_first[first, last)内であれば、std::copy_backwardstd::copyの代わりに使用する必要があります.
Original:
the beginning of the destination range. If d_first is within [first, last), std::copy_backward must be used instead of std::copy.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pred -
必要な要素のために
Original:
for the required elements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
true を返す単項述語。

述語関数のシグネチャは以下と同等なものであるべきです。

 bool pred(const Type &a);

シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。
Type は型 InputIt のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

型の要件
-
InputItInputIterator の要件を満たさなければなりません。
-
OutputItOutputIterator の要件を満たさなければなりません。

[編集]値を返します

先の範囲内の要素への出力反復子は、最後の要素の1は、コピーされた.
Original:
Output iterator to the element in the destination range, one past the last element copied.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]複雑性

1)
まさにlast - first割り当て
Original:
Exactly last - first assignments
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
述語の正確last - firstアプリケーション
Original:
Exactly last - first applications of the predicate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]ノート

実際には、std::copyの実装では、バルク·コピーなどstd::memcpyなどの関数値の型はTriviallyCopyableである場合、複数の割り当てと使用を避ける
Original:
In practice, implementations of std::copy avoid multiple assignments and use bulk copy functions such as std::memcpy if the value type is TriviallyCopyable
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]可能な実装

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;}

[編集]

次のコードは、別の両​​方のコピーに1ベクトルの内容をコピーし、その結果のベクトルを表示する:
Original:
The following code uses copy to both copy the contents of one vector to another and to display the resulting vector:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>#include <iostream>#include <vector>#include <iterator>   int main(){std::vector<int> from_vector;for(int i =0; i <10; i++){ from_vector.push_back(i);}   std::vector<int> to_vector(10);   std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());   std::cout<<"to_vector contains: "; std::copy(to_vector.begin(), to_vector.end(), std::ostream_iterator<int>(std::cout, " "));std::cout<<std::endl;}

出力:

to_vector contains: 0 1 2 3 4 5 6 7 8 9

[編集]参照

指定範囲の要素を後ろからコピーします
(関数テンプレート)[edit]
指定範囲の要素から一定の基準を満たすものを除いてコピーします
(関数テンプレート)[edit]
close