Boost logo

Boost Users :

Subject: Re: [Boost-users] [lambda] Defining a predicate for std::find_if if the key to a std::map is std::pair
From: Ryan (mccorywork_at_[hidden])
Date: 2009-09-24 23:40:31


On Thu, Sep 24, 2009 at 7:04 PM, Anthony Foglia <AFoglia_at_[hidden]>wrote:

> I don't think it's that bad. The retrieval will be only slightly worse
> with an extra ".second" you'd need to add to get the vector of classA.

I would of course have to compare the int in that pair and if it matches
return the extra ".second". Not bad but not optimal.

> Yes, but it also will only compare the string value and not the int when
> using map::find as well.
>

I think by provided the LessFirstOfPair to the map gives the best of both
worlds. When using the map functions I only compare the name value of the
key. When needing to retrieve the value I can use std::find_if and by pass
the given comparison. That gives the following code.

Set up

> typedef map< pair<string,int>, vector<classA>, LessFirstOfPair > cMap1;
>

Insersion
std::pair<cMap1::iterator, bool> inserted =
   map1.insert(cMap1::value_type(cMap::key_type(givenName,
properInt),cMap1::mapped_type()));

Retrieval

> find_if(map1.begin(), map1.end(),
> bind(&cMap1::value_type::first, _1)
> ==make_pair(givenName, intValue));\
>

Thanks for the help.

Ryan



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net