std::copy, std::copy_if
De cppreference.com
<metanoindex/>
<tbody> </tbody>| Definido no cabeçalho <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) | (desde C++11) |
Copia os elementos no intervalo, definidos por
[first, last), a outra no início gama d_first. A segunda função apenas copia os elementos para os quais o predicado pred retornos 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.
Parâmetros
| first, last | - | a gama de elementos para copiar
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 | - | o início do intervalo de destino. Se
d_first está dentro [first, last), std::copy_backward deve ser utilizado em vez de 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 para os elementos necessários . 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 |
| Type requirements | ||
-InputIt must meet the requirements of InputIterator.
| ||
-OutputIt must meet the requirements of OutputIterator.
| ||
Valor de retorno
Iterator saída para o elemento da faixa de destino, um após o último elemento copiado.
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.
Complexidade
1)
Exatamente
last - first atribuiçõesOriginal:
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.
2)
Exatamente
last - first aplicações do predicadoOriginal:
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.
Notas
Na prática, as implementações de
std::copy evitar várias atribuições e uso em massa cópia funções como std::memcpy se o tipo de valor é TriviallyCopyableOriginal:
In practice, implementations of
std::copy avoid multiple assignments and use bulk copy functions such as std::memcpy if the value type is TriviallyCopyableThe 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.
Possível implementação
| 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;
}
|
Exemplo
O código a seguir usa cópia para a cópia do conteúdo de um vetor para outro e para exibir o vetor resultante:
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;
}
Saída:
to_vector contains: 0 1 2 3 4 5 6 7 8 9
