...one of the most highly
regarded and expertly designed C++ library projects in the
world.

— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards

template< typename Sequence , typename Pred , typename NewType > struct replace_if { typedefunspecifiedtype; };

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.

#include "boost/mpl/replace_if.hpp"

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 | 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;` . |

Linear. Performs exactly `size<Sequence>::type::value`

applications of `Pred`

, and at most `size<Sequence>::type::value`

insertions.

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));

Algorithms, `replace`

, `transform`

Table of Contents

Last edited March 12, 2003 6:32 am