std::remove, std::remove_if
De 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. |
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) | |
Quita todos los elementos que cumplan criterios específicos de la
[first, last)
rango. La primera versión elimina todos los elementos que son iguales a value
, la segunda versión elimina todos los elementos para los que predicado p
vuelve 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.
Extracción se realiza desplazando los elementos en el intervalo de tal manera que los elementos que se borren se sobrescriben. Los elementos entre los antiguos y los nuevos fines de la gama tienen valores no especificados. Un iterador al nuevo extremo de la gama se devuelve. Orden relativo de los elementos que permanecen se conserva .
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.
Contenido |
[editar] Parámetros
first, last | - | el intervalo de elementos de proceso
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 | - | el valor de los elementos que desea eliminar
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 si el elemento debe ser eliminado . 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 .
|
[editar] Valor de retorno
Iterator al nuevo extremo de la gama
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.
[editar] Complejidad
Exactamente std::distance(first, last) aplicaciones del predicado .
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.
[editar] Notas
Las funciones de contenedores con nombres similares miembro list::remove, list::remove_if, forward_list::remove y forward_list::remove_if borrar los elementos eliminados .
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.
[editar] Posible implementación
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; } |
[editar] Ejemplo
El código siguiente quita todos los espacios de una cadena moviéndolos hasta el final de la cadena y luego borrar .
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
[editar] Ver también
Copia un intervalo de elementos omitiendo los que satisfacen criterios específicos 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. (función de plantilla) |