Question

I want to implement an ifexists() template function which checks whether a key exists in a map or not.

If it is a std::map< > , I can use find() function in template, and hence can implement my template ifexists() function. (below)

But I use Boost::associative_property_map which references my std::map. I can use get and put functions on this associative property map.

std::map< int, int > mymap;
boost::associative_property_map< std::map< int, int > > asso_map ( mymap );

//asso_map stores reference to mymap. and i can use get and put functions on this associative map.

Now, if I pass this asso_map to my template function " ifexists( asso_map, key) ", it throws me the below error as:

error: âclass boost::associative_property_map<std::map<int, int > >â has no member named âendâ
error: âclass boost::associative_property_map<std::map<int, int > >â has no member named âfindâ

Here is my template function code:

template <typename map_t, typename key_t>
bool exists_in(map_t map, key_t key)  
{  
  return map.find(key) != map.end();
}

//function call:--> v is int.
if( exists_in ( asso_map, v) ) ... Error (above) 
if( exists_in ( mymap, v) ) ... Correct 

The error that it is no member function as "find" is correct and after seeing documentation here, it is clear that it has no such member function as find().

However if I pass actual map ( i.e. mymap ) to template function, it works. But I dont want to do that because then there is no point of using associative property map of boost.

I wanted to find a way where I could use find() for my associative_property_map asso_map. I know I could iterate over assoc_map and find my way out. but wondering if I could somehow use find()

I hope it is more clear. Sorry for the ambiguity.

Was it helpful?

Solution

Unfortunately, boost::associative_property_map does not give access to the associative container it uses. You cannot access the find() function of the std::map, or iterate over the content of it. You only have access to the API of Lvalue Property Map.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top