|
Boost Users : |
From: Thorsten Ottosen (tottosen_at_[hidden])
Date: 2006-02-09 05:09:18
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 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