Difference between revisions of "cpp/algorithm/replace"

< cpp‎ | algorithm
(the example had three compilation errors (missing template arg for array, unknown identifier endl, incorrect use of bind()). Works now with gcc 4.6.0)
(Example: _must_ return)
Line 83: Line 83:
     std::cout << '\n';
     std::cout << '\n';
    return 0;
  | output=
  | output=

Revision as of 13:40, 23 August 2011

Template:cpp/algorithm/sidebar Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <algorithm>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIterator, class T >

void replace( ForwardIterator first, ForwardIterator last,

              const T& old_value, const T& new_value );

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIterator, class UnaryPredicate, class T >

void replace_if( ForwardIterator first, ForwardIterator last,

                 UnaryPredicate p, const T& new_value );

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Replaces all elements satisfying specific criteria with new_value in the range [first, last). The first version replaces the elements that are equal to old_value, the second version replaces elements for which predicate p returns Template:cpp.



first, last - the range of elements to process
old_value - the value of elements to replace
p - unary predicate which returns ​true if the element value should be replaced.

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.
The type Type must be such that an object of type ForwardIterator can be dereferenced and then implicitly converted to Type. ​

new_value - the value to use as replacement

Return value

Template:return none


Exactly last - first applications of the predicate.

Equivalent function

Template:eq fun cpp


Template:example cpp

See also

Template:cpp/algorithm/dcl list replace copy