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 for the latest Boost documentation.

[Home]copy_if

Synopsis

template<
      typename Sequence
    , typename State
    , typename BinaryOp
    , typename Pred
    >
struct copy_if
{
    typedef unspecified type;
};

Description

Returns the result of the successive application of BinaryOp to the result of the previous BinaryOp invocation (State if it's the first call) and every element in the range [begin<Sequence>::type,end<Sequence>::type) that satisfies the predicate Pred, in the linear order. A typical application for copy_if is to conditionally copy the content of one sequence into another - see the example below.

Definition

#include "boost/mpl/copy_if.hpp"

Parameters

 Parameter  Requirement  Description  
SequenceA model of SequenceA sequence to iterate.
StateA typeThe initial state for the first BinaryOp application.
BinaryOpA model of [Lambda Function]The operation to be executed on forward traversal.
PredAn unary Predicate [Lambda Expression]The copying condition.

Expression semantics

 Expression  Expression type  Precondition  Semantics  Postcondition 
typedef copy_if<Sequence,T,Op,Pred>::type s;A typeEquivalent to typedef lambda<Op>::type op; typedef lambda<Pred>::type pred; typedef fold< Sequence,T,if_< apply<pred,_2>, apply<op,_1,_2>, _1 > >::type s;.

Complexity

Linear. Exactly size<Sequence>::type::value applications of Pred, and at most size<Sequence>::type::value applications of BinaryOp.

Example

typedef list_c<int,0,1,2,3,4,5,6,7,8,9>::type numbers;
typedef list_c<int,0,1,2,3,4>::type answer;
typedef copy_if<
      numbers
    , vector_c<int>
    , push_back<_1,_2>
    , less<_1,int_<5> >
    >::type result;

BOOST_STATIC_ASSERT(size<result>::value == 5); BOOST_STATIC_ASSERT((equal<result,answer>::type::value));

See also

Algorithms, copy, copy_backward_if, copy_backward, fold, iter_fold


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