0

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.

Pogo
  • 475
  • 3
  • 19
  • What is the question? From your message it is not clear. What is more, your code mixes C++ code and comments that are not really formatted as C++ comments. Can you please edit your question to address those two issues? – lrineau Feb 03 '14 at 16:14
  • Hi, I have updated the question. I hope it is more clear now. – Pogo Feb 03 '14 at 18:05

1 Answers1

0

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.

lrineau
  • 6,036
  • 3
  • 34
  • 47
  • I know, that kind of hampers additional functionality. I asked the same question on boost-users list and awaiting their response. If they could add this interface, that would be very useful. There should be some reason why they did not provide such interface. Get, Put and Find are very important for using property maps. – Pogo Feb 04 '14 at 15:28
  • @user2404319 If the Boost mailing-list gives your an answer that is more useful, please post another answer here, or edit my answer. – lrineau Feb 04 '14 at 15:49