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

std::copy, std::copy_if

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を非改変
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を変更する
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
操作を仕切る
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)ソート操作
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
バイナリ検索操作(ソート範囲で)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)操作を設定します
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ヒープ操作
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最小値/最大値操作
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
数値演算
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Cライブラリ
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <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 - unary predicate which returns ​true
必要な要素のために
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.
.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const&, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

型の要件
-
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.

[編集]可能な実装

First version
template<class InputIt, class OutputIt> OutputIt copy(InputIt first, InputIt last, OutputIt d_first){while(first != last){*d_first++=*first++;}return d_first;}
Second version
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

[編集]参照

コピー下位の順序で要素の範囲
Original:
copies a range of elements in backwards order
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(関数テンプレート)[edit]
特定の条件を満たすものを省略要素の範囲をコピーします
Original:
copies a range of elements omitting those that satisfy specific criteria
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(関数テンプレート)[edit]
close