copy(V.begin(), V.end(), ostream_iterator<int>(cout, ' '));

// The output is '1 5 7'.

Notes

[1] The meaning of 'removal' is somewhat subtle. Remove_if does not destroy any iterators, and does not change the distance between first and last. (There's no way that it could do anything of the sort.) So, for example, if V is a vector, remove_if(V.begin(), V.end(), pred) does not change V.size(): V will contain just as many elements as it did before. Remove_if returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest, and may be discarded. If you are removing elements from a Sequence, you may simply erase them. That is, a reasonable way of removing elements from a Sequence is S.erase(remove_if(S.begin(), S.end(), pred), S.end()) .

See also

remove, remove_copy, remove_copy_if, unique, unique_copy.

remove_copy

Category: algorithms

Component type: function

Prototype

template <class InputIterator, class OutputIterator, class T>

OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);

Description

Remove_copy copies elements that are not equal to value from the range [first, last) to a range beginning at result. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as in the range [first, last).

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

• InputIterator is a model of Input Iterator.

• OutputIterator is a model of Output Iterator.

• InputIterator's value type is convertible to a type in OutputIterator's set of value types.

• T is a model of Equality Comparable.

• Objects of type T can be compared for equality with objects of InputIterator's value type.

Preconditions

• [first, last) is a valid range.

• There is enough space in the output range to store the copied values. That is, if there are n elements in [first, last) that are not equal to value, then [result, result+n) is a valid range.

• result is not an iterator in the range [first, last) .

Complexity

Linear. Exactly last – first comparisons for equality, and at most last – first assignments.

Example

Print all nonzero elements of a vector on the standard output.

vector<int> V;

V.push_back(-2);

V.push_back(0);

V.push_back(-1);

V.push_back(0);

V.push_back(1);

V.push_back(2);

remove_copy(V.begin(), V.end(), ostream_iterator<int>(cout, ' '), 0);

See also

copy, remove, remove_if, remove_copy_if, unique, unique_copy.

remove_copy_if

Category: algorithms

Component type: function

Prototype

template <class InputIterator, class OutputIterator, class Predicate>

OutputIterator remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);

Description

Remove_copy_if copies elements from the range [first, last) to a range beginning at result , except that elements for which pred is true are not copied. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as in the range [first, last).

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

• InputIterator is a model of Input Iterator.

• OutputIterator is a model of Output Iterator.

• InputIterator's value type is convertible to a type in OutputIterator's set of value types.

• Predicate is a model of Predicate.

• InputIterator's value type is convertible to Predicate's argument type.

Preconditions

• [first, last) is a valid range.

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату