std::remove, std::remove_if
Материал из 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. |
Заголовочный файл <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) | |
Удаляет все элементы, удовлетворяющие определенным критериям из серии
[first, last)
. Первая версия удаляет все элементы, которые равны value
, вторая версия удаляет все элементы, для которых предикат возвращает p
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.
Удаление производится путем перемещения элементов в диапазоне таким образом, что элементы должны быть удалены, будут перезаписаны. Элементов между старым и новым концах диапазона имеют неопределенное значение. Итератор к новому конце диапазона возвращается. Относительный порядок элементов, которые остаются сохраняется.
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.
Содержание |
[править] Параметры
first, last | - | диапазон элементов в процессе
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 | - | Значение элементов для удаления
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 если элемент должен быть удален . 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:
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 .
|
[править] Возвращаемое значение
Iterator к новому конце диапазона
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.
[править] Сложность
Именно std::distance(first, last) применения предикатов.
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.
[править] Notes
Аналогичным названием функции контейнера член list::remove, list::remove_if, forward_list::remove, и forward_list::remove_if стереть удаленные элементы.
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.
[править] Возможная реализация
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; } |
[править] Пример
Следующий код удаляет все пробелы из строки, перемещая их в конец строки, а затем стирание .
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'; }
Вывод:
Textwithsomespaces
[править] См. также
Копирует диапазон элементов опуская те, которые удовлетворяют определенным критериям 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. (шаблон функции) |