std::copy, std::copy_if
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Elemento definito nell'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) | (dal C++11) |
Copia gli elementi della gamma, definiti da
[first, last)
, a un altro inizio gamma a d_first
. La seconda funzione copia solo gli elementi per i quali il predicato pred
ritorna 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.
Indice |
[modifica] Parametri
first, last | - | dell'intervallo di elementi da copiare
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 | - | l'inizio del campo di destinazione. Se
d_first è all'interno [first, last) , std::copy_backward deve essere usato al posto di 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 per gli elementi richiesti . 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: bool pred(const Type &a); 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 .
|
[modifica] Valore di ritorno
Iteratore di uscita per l'elemento nel campo di destinazione, un passato l'ultimo elemento copiato.
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.
[modifica] Complessità
1)Esattamente
2) last - first
assegnazioniOriginal:
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.
Esattamente
last - first
applicazioni del predicatoOriginal:
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.
[modifica] Note
In pratica, le implementazioni di
std::copy
evitare assegnazioni multiple e l'uso di copia di massa funzioni come std::memcpy se il tipo di valore è 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.
[modifica] Possibile implementazione
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; } |
[modifica] Esempio
Il codice seguente utilizza copia sia a copiare il contenuto di un vettore ad un altro e per visualizzare il vettore risultante:
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
[modifica] Vedi anche
una serie di elementi in ordine inverso 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. (funzione di modello) | |
copia un intervallo di elementi omettendo quelli che soddisfano criteri specifici 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. (funzione di modello) |