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 an old version of boost. Click here for the latest version's documentation home page.

[Home]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  
SequenceA model of Extensible SequenceThe original sequence.
PredAn unary Predicate [Lambda Expression]The replacement condition.
NewTypeA typeA type to replace with.

Expression semantics

 Expression  Expression type  Precondition  Semantics  Postcondition 
typedef replace_if<Sequence,Pred,NewType>::type s;A model of Extensible SequenceEquivalent 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_<0> >::type result;
typedef list_c<int,1,4,0,2,0,0,3,0>::type answer;
BOOST_STATIC_ASSERT((equal< answer,result >::type::value));

See also

Algorithms, replace, transform


Table of Contents
Last edited March 12, 2003 6:32 am