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

Front Page / Algorithms / Transformation Algorithms / replace



      typename Sequence
    , typename OldType
    , typename NewType
    , typename In = unspecified
struct replace
    typedef unspecified type;


Returns a copy of the original sequence where every type identical to OldType has been replaced with NewType.

[Note: This wording applies to a no-inserter version(s) of the algorithm. See the Expression semantics subsection for a precise specification of the algorithm's details in all cases — end note]


#include <boost/mpl/replace.hpp>

Model of

Reversible Algorithm


Parameter Requirement Description
Sequence Forward Sequence A original sequence.
OldType Any type A type to be replaced.
NewType Any type A type to replace with.
In Inserter An inserter.

Expression semantics

The semantics of an expression are defined only where they differ from, or are not defined in Reversible Algorithm.

For any Forward Sequence s, an Inserter in, and arbitrary types x and y:

typedef replace<s,x,y,in>::type r;
Return type:

A type.


Equivalent to

typedef replace_if< s,y,is_same<_,x>,in >::type r;


Linear. Performs exactly size<s>::value comparisons for identity / insertions.


typedef vector<int,float,char,float,float,double> types;
typedef vector<int,double,char,double,double,double> expected;
typedef replace< types,float,double >::type result;

BOOST_MPL_ASSERT(( equal< result,expected > ));

See also

Transformation Algorithms, Reversible Algorithm, reverse_replace, replace_if, remove, transform