Boost logo

Boost :

Subject: Re: [boost] Search a vector of tuples?
From: Robert Dailey (rcdailey.lists_at_[hidden])
Date: 2012-04-23 17:15:21


On Thu, Apr 12, 2012 at 6:13 AM, Mathias Gaunard <
mathias.gaunard_at_[hidden]> wrote:

> On 12/04/12 10:36, Robert Jones wrote:
>
>> On Wed, Apr 11, 2012 at 11:33 PM, Robert Dailey<rcdailey.lists_at_gmail.**
>> com <rcdailey.lists_at_[hidden]>>wrote:
>>
>> Is there a method in the boost library to iterate a vector of
>>> boost::tuple
>>> objects, comparing the 1st element in the tuple of each tuple to a value
>>> that I specify? My tuple is setup like so:
>>>
>>> typedef boost::tuple<int, int, char const*> Tuple;
>>> typedef std::vector<Tuple> ErrorStringMap;
>>>
>>> ErrorStringMap mystrings = tuple_list_of
>>> (10, "10", "ten")
>>> (20, "20", "twenty")
>>> (30, "30", "thirty")
>>> (10, "10", "ten");
>>>
>>> I want to search the vector of tuples and do something with every tuple
>>> that has the number 10 as the first element in the tuple.
>>>
>>>
>>> *** Not Tested or Compiled ***
>>
>> #include<boost/range.hpp>
>> #include<boost/range/adaptors/**filtered.hpp>
>> // ... and maybe some other range headers
>> #include<boost/bind.hpp>
>>
>> for_each( mystrings | boost::adaptors::filtered( boost::bind( get<0>, _1 )
>> == 10 ), yourAction );
>>
>
> You probably can't take the address of get<0>.
>
> Also, you could remove the dependence on filtered by using find_if instead
> of for_each.

I can't use phoenix, I'm still on boost 1.46.1.

I try the following with boost::lambda:

    using namespace boost::lambda;
    using boost::lambda::_1;
    using boost::lambda::bind;

    ErrorStringMap::const_iterator it =
        std::find_if( mystrings.begin(), mystrings.end(),
bind(&Tuple::get<0>, _1) == error_code );

    if( it != mystrings.end() ) {
        return *it;
    }

However I keep getting compiler error C2780, saying it's expecting N number
of arguments. I'm on MSVC9. Why can't I get lambda to work??


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk