std::remove, std::remove_if
Aus 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. |
definiert 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) | |
Entfernt alle Elemente, die spezifische Kriterien aus dem Bereich
[first, last)
erfüllen. Die erste Version entfernt alle Elemente, die gleich value
sind, die zweite Version entfernt alle Elemente für die das Prädikat p
true zurückgibt. 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.
Das Entfernen geschieht durch ein Verschieben aller Elemente des Bereiches, so dass Elemente, die gelöscht werden sollen, überschrieben werden. Die Elemente zwischen dem alten und dem neuen Ende des Bereiches haben unbestimmte Werte. Ein Iterator zum neuen Ende des Bereichs wird zurückgegeben. Die relative Reihenfolge der übrigen Elemente bleibt erhalten .
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.
Inhaltsverzeichnis |
[Bearbeiten] Parameter
first, last | - | Der Bereich der zu verarbeitenden Elemente
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 | - | Der Wert der Elemente die zu entfernen sind
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 ob das Element entfernt werden soll . 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 .
|
[Bearbeiten] Rückgabewert
Iterator zum neuen Ende des Bereichs
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.
[Bearbeiten] Komplexität
Genau std::distance(first, last) Anwendungen des Prädikats .
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.
[Bearbeiten] Notes
Die ähnlich benannten Behälter Elementfunktionen list::remove, list::remove_if, forward_list::remove und forward_list::remove_if löscht die entfernten Elemente .
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.
[Bearbeiten] Mögliche Implementierung
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; } |
[Bearbeiten] Beispiel
Der folgende Code entfernt alle Leerzeichen aus einem String, indem sie bis zum Ende des Strings und dann Löschen .
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
[Bearbeiten] Siehe auch
Kopiert einen Bereich von Elementen ohne die Seiten, die bestimmte Kriterien erfüllen 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. (Funktions-Template) |