std::copy, std::copy_if
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
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, | (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.
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_backwardはstd::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. |
型の要件 | ||
-InputIt は InputIterator の要求を満足しなければなりません。 | ||
-OutputIt は OutputIterator の要求を満足しなければなりません。 |
[編集]値を返します
先の範囲内の要素への出力反復子は、最後の要素の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.
You can help to correct and verify the translation. Click here for instructions.
[編集]複雑性
1)まさに
2) last - first
割り当てOriginal:
Exactly
last - first
assignmentsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
述語の正確
last - first
アプリケーションOriginal:
Exactly
last - first
applications of the predicateThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
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.
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.
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. (関数テンプレート) | |
特定の条件を満たすものを省略要素の範囲をコピーします 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. (関数テンプレート) |