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. |
Заголовочный файл <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.
[править] Notes
На практике реализация
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; }
Вывод:
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. (шаблон функции) |