◐ Shell
clean mode source ↗

std::copy, std::copy_if — cppreference.com

De cppreference.com

<metanoindex/>

<tbody> </tbody>

Déclaré dans l'en-tête

<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) (depuis C++11)

Copier les éléments de la gamme, défini par [first, last), dans un autre intervalle au début d_first. La deuxième fonction ne copie que les éléments pour lesquels le prédicat renvoie 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.

Paramètres

first, last -

l'éventail des éléments à copier

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 -

le début de la plage de destination. Si d_first est dans [first, last), std::copy_backward doit être utilisé à la place 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 - prédicat unéaire qui retourne ​true

pour les éléments requis

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.

.

L'expression pred(v) doit pouvoir être convertie à bool pour tout argument v de type (possiblement const) VT, où VT est le type de valeur de InputIt, inconditionellement de value category, et ne doit pas modifier v. Ainsi, un type-paramètre VT&n'est pas permis ainsi que pour VT sauf pour VT un déplacement est équivalent à une copie (depuis C++11). ​

Type requirements
-InputIt must meet the requirements of InputIterator.
-OutputIt must meet the requirements of OutputIterator.

Retourne la valeur

Itérateur de sortie de l'élément dans la zone de destination, une après le dernier élément copié .

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.

Complexité

1)

Exactement last - first missions

Original:

Exactly last - first assignments

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

2)

Exactement last - first applications du prédicat

Original:

Exactly last - first applications of the predicate

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Notes

Dans la pratique, les implémentations de std::copy évitent les affectations multiples et utilisent des fonctions de copie binaire telles que std::memmove si le type de valeur est 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.

Mise en œuvre possible

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;
}

Exemple

Le code suivant utilise la copie à la fois sur le contenu d'un vecteur à l'autre et d'afficher le vecteur résultant:

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.

#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;
}

Résultat :

to_vector contains: 0 1 2 3 4 5 6 7 8 9

Voir aussi

copie une plage d'éléments dans l'ordre inverse

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.


(fonction générique) [edit]

Copie une série d'éléments en omettant ceux qui satisfont à certains critères

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.


(fonction générique) [edit]