Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext
replace_if
Description

Replaces each element of a given sequence for which an unary function object evaluates to true replaced with a new value.

Synopsis
template<
    typename Sequence,
    typename F,
    typename T>
typename result_of::replace_if<Sequence const, F, T>::type replace_if(
    Sequence const& seq, F f, T const& new_value);

Table 1.58. Parameters

Parameter

Requirement

Description

seq

A model of Forward Sequence

Operation's argument

f

A function object for which f(e) is a valid expression, convertible to bool, for each element e in seq

Operation's argument

new_value

Any type

Replacement value

Expression Semantics
replace_if(seq, f, new_value);

Return type: A model of Forward Sequence.

Semantics: Returns a new sequence with all the elements of seq, with new_value assigned to each element for which f evaluates to true.

Complexity

Constant. Returns a view which is lazily evaluated.

Header
#include <boost/fusion/algorithm/transformation/replace_if.hpp>
#include <boost/fusion/include/replace_if.hpp>
Example
struct odd
{
    template<typename T>
    bool operator()(T t) const
    {
        return t % 2;
    }
};
...
assert(replace_if(make_vector(1,2), odd(), 3) == make_vector(3,2));

PrevUpHomeNext