Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use boost::lambda together with std::find_if?

Tags:

c++

I have a std::vector and I want to check a specific attribute of each element. SomeStruct has an attribute 'type'. I want to check this attribute to be either Type1 or Type2.

My plan is to use boost::lambda.

std::vector<SomeStruct>::const_iterator it =
    std::find_if(
        vec.begin(), vec.end(),
        _1.type == SomeStruct::Type1 || _1.type == SomeStruct::Type2);

Because I need to access a specific attribute of each element, I'm not sure if I can use boost::lambda at all.

Any hints?

like image 997
Jens Luedicke Avatar asked Aug 28 '09 10:08

Jens Luedicke


2 Answers

std::find_if(
    vec.begin(), vec.end(),
    bind(&SomeStruct::type, _1) == SomeStruct::Type1 ||
    bind(&SomeStruct::type, _1) == SomeStruct::Type2);
like image 114
sepp2k Avatar answered Nov 13 '22 11:11

sepp2k


Your expression does not compile because of

_1.type

The dot operator cannot be overloaded so your expression cannot work as a lambda expression, It's simply referring to member type of the object _1 defined in boost::lambda.hpp. Well, I don't know what is _1 type and thinking about this type makes me shudder - it's not for us, mortals to know it :-).
The proper expression is given by sepp2k.

like image 28
Tadeusz Kopec for Ukraine Avatar answered Nov 13 '22 10:11

Tadeusz Kopec for Ukraine