|
Boost Users : |
From: Bill Buklis (boostuser_at_[hidden])
Date: 2006-02-09 13:01:02
Nevermind. I see you did put the predicate in your message. Thanks. Somehow
I missed it when reading it.
bool predicate( const std::pair<const std::string,void*>& p )
{
return p.first.size() > 3;
}
This does work correctly. Thanks.
Out of curiosity, if nothing else, is there a corresponding typedef that
would match this? std::map would use value_type. What would be the
equivalent for the ptr_map/void-ptr-map? Of course, depending on the length
and indirection, it may be better anyway to explicitly list it out as the
std::pair.
-- Bill --
-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Thorsten Ottosen
Sent: Thursday, February 09, 2006 4:09 AM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [ptr_container] ptr_map predicates
Bill Buklis wrote:
> How would I accomplish the following using ptr_map instead of std::map?
>
> typedef std::map< std::string, int* > stlmap;
>
> bool predicate( stlmap::value_type& mapitem )
> {
> // return true if key value has at least 4 characters
> return( mapitem.first.size() > 3 );
> }
>
> void sample( stlmap& themap )
> {
> std::find_if( themap.begin(), themap.end(), predicate );
> }
>
>
> The predicate for std::find_if requires *iterator for the parameter.
> However, with ptr_map *iterator returns a reference to the element value
> instead of the key/value pair. Normally this is fine (in general code I
like
> this feature), but in this case I need access to the key in the predicate.
> How do I get at it?
this is an interesting use-case that I haven't thought of.
> ptr_map version of above:
>
> typedef boost::ptr_map< std::string, int > bpmap;
>
> bool predicate( ??? )
> {
> ???
> }
bool predicate( const std::pair<const std::string,void*>& p )
{
return p.first.size() > 3;
}
> void sample( bpmap& themap )
> {
> std::find_if( ??? );
> }
void sample( bpmap& m )
{
std::find_if( m.begin().base(), m.end().base(), predicate() );
}
iterator.base() should return an iterator to the underlying void-ptr-map.
I hope it works
-Thorsten
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
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