std::copy, std::copy_if
来自cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
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:
The signature does not need to have const &, but the function must not modify the objects passed to it. | |||||||||
Type requirements | |||||||||||
-InputIt must meet the requirements of InputIterator .
| |||||||||||
-OutputIt must meet the requirements of OutputIterator .
|
[编辑] 返回值
输出迭代器在目标范围内的元素,复制过去的最后一个元素.
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; } |
[编辑] 为例
下面的代码复制到一个向量和显示的内容都复制得到的载体
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; }
Output:
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. (函数模板) |