# replace_if

### Synopsis

    template<
          typename Sequence
        , typename Pred
        , typename NewType
        >
    struct replace_if
    {
        typedef unspecified type;
    };

### Description

Performs a conditional replacement operation on the sequence. The algorithm returns a new sequence which contains all the elements from `[begin<Sequence>::type, end<Sequence>::type)` range where every type that satisfies the predicate `Pred` has been replaced with a `NewType`. The result sequence preserves all the functional and performance characteristics of the original `Sequence`, including its size, but not identity.

### Definition

    #include "boost/mpl/replace_if.hpp"

### Parameters

Parameter | Requirement | Description
--- | --- | ---
`Sequence` | A model of Extensible Sequence | The original sequence.
`Pred` | An unary Predicate [Lambda Expression] | The replacement condition.
`NewType` | A type | A type to replace with.

### Expression semantics

Expression | Expression type | Precondition | Semantics | Postcondition
--- | --- | --- | --- | ---
`typedef replace_if<Sequence,Pred,NewType>::type s;` | A model of Extensible Sequence |  | Equivalent to `typedef lambda<Pred>::type pred; typedef transform< Sequence, if_< apply1<pred,_1>,NewType,_1> >::type t;`. |

### Complexity

Linear. Performs exactly `size<Sequence>::type::value` applications of `Pred`, and at most `size<Sequence>::type::value` insertions.

### Example

    typedef list_c<int,1,4,5,2,7,5,3,5>::type numbers;
    typedef replace_if< numbers, greater<_,4>, int_c<0> >::type result;
    typedef list_c<int,1,4,0,2,0,0,3,0>::type answer;
    BOOST_STATIC_ASSERT((equal< answer,result,equal_to<_,_> >::type::value));

### See also

Algorithms, `replace`, `transform`

Table of Content | Reference
Last edited July 17, 2002 5:38 am