Thursday, June 13, 2013

A easy way to sort based on boost::bind


std::sort( first, last, bind( &X::name, _1 ) < bind( &X::name, _2 ) ); // sort by name

Example:

    //  find score difference Sort the vector by the first element increasingly


    std::vector<std::pair<int, ColorCategory> > labelColorPairs;
    std::sort(labelColorPairs.begin(), labelColorPairs.end(),
              boost::bind(&std::pair<int,ColorCategory>::first, _1) <
              boost::bind(&std::pair<int,ColorCategory>::first, _2));
Other bind functions (overload operators)

std::remove_if( first, last, !bind( &X::visible, _1 ) ); // remove invisible objects
and compare the result of bind against a value:
std::find_if( first, last, bind( &X::name, _1 ) == "Peter" );
std::find_if( first, last, bind( &X::name, _1 ) == "Peter" || bind( &X::name, _1 ) == "Paul" );
You can find them in details:

No comments:

Post a Comment