std::copy, std::copy_if
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
ヘッダ <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 | - | 必要な要素のために 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. 述語関数のシグネチャは以下と同等なものであるべきです。 bool pred(const Type &a); シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。 |
型の要件 | ||
-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.
[編集]可能な実装
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.
You can help to correct and verify the translation. Click here for instructions.
Run this code
#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
[編集]参照
指定範囲の要素を後ろからコピーします (関数テンプレート) | |
指定範囲の要素から一定の基準を満たすものを除いてコピーします (関数テンプレート) |