std::remove, std::remove_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. |
Defined in header <algorithm> | ||
template<class ForwardIt, class T > ForwardIt remove( ForwardIt first, ForwardIt last, const T& value ); | (1) | |
template<class ForwardIt, class UnaryPredicate > ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p ); | (2) | |
Rimuove tutti gli elementi che soddisfano i criteri specifici della
[first, last)
gamma. La prima versione rimuove tutti gli elementi che sono uguali a value
, la seconda versione rimuove tutti gli elementi per i quali predicato p
ritorna true. Original:
Removes all elements satisfying specific criteria from the range
[first, last)
. The first version removes all elements that are equal to value
, the second version removes all elements for which predicate p
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.
Rimozione avviene spostando gli elementi nell'intervallo in modo tale che gli elementi da cancellare vengono sovrascritti. Gli elementi tra la vecchia e la nuova estremità della gamma hanno valori non specificati. Un iteratore alla fine della nuova gamma è restituito. Ordine relativo degli elementi che rimangono è conservato.
Original:
Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten. The elements between the old and the new ends of the range have unspecified values. An iterator to the new end of the range is returned. Relative order of the elements that remain is preserved.
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 | - | l'intervallo di elementi di processo Original: the range of elements to process The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
value | - | il valore di elementi da rimuovere Original: the value of elements to remove The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
p | - | unary predicate which returns true se l'elemento deve essere rimosso . Original: if the element should be removed 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 | ||
-ForwardIt must meet the requirements of ForwardIterator . | ||
-The type of dereferenced ForwardIt must meet the requirements of MoveAssignable . |
[modifica]Valore di ritorno
Iterator alla fine della nuova gamma
Original:
Iterator to the new end of the range
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à
Esattamente std::distance(first, last) applicazioni del predicato.
Original:
Exactly std::distance(first, last) 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.
You can help to correct and verify the translation. Click here for instructions.
[modifica]Note
Analogamente il nome funzioni membro contenitore list::remove, list::remove_if, forward_list::remove, e forward_list::remove_if cancellare gli elementi rimossi.
Original:
The similarly-named container member functions list::remove, list::remove_if, forward_list::remove, and forward_list::remove_if erase the removed elements.
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 ForwardIt, class T> ForwardIt remove(ForwardIt first, ForwardIt last, const T& value){ ForwardIt result = first;for(; first != last;++first){if(!(*first == value)){*result++=*first;}}return result;} |
Second version |
template<class ForwardIt, class UnaryPredicate> ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p){ ForwardIt result = first;for(; first != last;++first){if(!p(*first)){*result++=*first;}}return result;} |
[modifica]Esempio
Il codice seguente rimuove tutti gli spazi da una stringa spostandoli alla fine della stringa e poi cancellando .
Original:
The following code removes all spaces from a string by moving them to the end of the string and then erasing.
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 <string>#include <iostream>int main(){std::string str ="Text with some spaces"; str.erase(std::remove(str.begin(), str.end(), ' '), str.end());std::cout<< str <<'\n';}
Output:
Textwithsomespaces
[modifica]Vedi anche
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) |