Replace every negative number with 0.

vector<int> V;

V.push_back(1);

V.push_back(-3);

V.push_back(2);

V.push_back(-1);

replace_if(V.begin(), V.end(), bind2nd(less<int>(), 0), –1);

assert(V[1] == 0 && V[3] == 0);

See also

replace, replace_copy, replace_copy_if

replace_copy

Category: algorithms

Component type: function

Prototype

template <class InputIterator, class OutputIterator, class T>

OutputIterator replace_copy (InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value);

Description

Replace_copy copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element equal to old_value is not copied; new_value is copied instead. More precisely, for every integer n such that 0 <= n < last-first, replace_copy performs the assignment *(result+n) = new_value if *(first+n) == old_value, and *(result+n) = * (first+n) otherwise.

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.

• T is EqualityComparable, and may be compared for equality with objects of InputIterator's value type.

• T is Assignable.

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

Preconditions

• [first, last) is a valid range.

• There is enough space in the output range to store the copied values. That is, [result, result + (last-first)) is a valid range.

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

Complexity

Linear. Replace_copy performs exactly last – first comparisons for equality and exactly last – first assignments.

Example

vector<int> V1;

V1.push_back(1);

V1.push_back(2);

V1.push_back(3);

V1.push_back(1);

vector<int> V2(4);

replace_copy(V1.begin(), V1.end(), V2.begin(), 1, 99);

assert(V[0] == 99 && V[1] == 2 && V[2] == 3 && V[3] == 99);

See also

copy, replace, replace_if, replace_copy_if

replace_copy_if

Category: algorithms

Component type: function

Prototype

template <class InputIterator, class OutputIterator, class Predicate, class T>

OutputIterator replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value)

Description

Replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element for which pred is true is not copied; new_value is copied instead. More precisely, for every integer n such that 0 <= n < last-first, replace_copy_if performs the assignment *(result+n) = new_value if pred(*(first+n)) , and *(result+n) = *(first+n) otherwise.

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.

• Predicate is a model of Predicate.

• T is convertible to Predicate's argument type.

• T is Assignable.

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

Preconditions

• [first, last) is a valid range.

• There is enough space in the output range to store the copied values. That is, [result, result + (last-first)) is a valid range.

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

0

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

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