Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
C++
template <class _Key, class _Tp, class _Compare=less<_Key> >
class IsMap : public map<_Key, _Tp, _Compare>
{
public:
_Tp& operator[](const _Key& __k) throw (ExptAppl)
{
for (IsMap<_Key, _Tp, _Compare>::iterator it = this->begin();
it != this->end(); it++)
if ((*it).first == __k)
return (*it).second;
string errMsg = "Element <" + __k + "> does not exist";
throw ExptAppl("", 0, 3003, "", (char*)errMsg.c_str());
}
};

Quote:
What I understood?

IsMap is a class template which inherits the class map. So here the generic Data type used here are:
_key -> This Denotes Key.
_Tp -> This Denotes Type.
_Compare=less<_key> -> A comparison function.
Now we need to understand how Map works?
std::map <key_type,data_type,[comparison>
The key comparison function, an ordering whose argument type is key_type; it returns true if its first argument is less than its second argument, and false otherwise. This is also defined as map::key_compare.This means that if you want to retrieve every key, value pair stored in the map, you can retrieve them in the order of the keys.

What I did not?
just the line : _Tp& operator[](const _Key& __k) throw (ExptAppl)
The Rest I have guessed.

Can you please help me understanding this whole piece of code by identifying this missing piece of logic?

Thanks
Deb
Posted
Comments
Philippe Mori 25-Nov-13 20:02pm    
map already have function to check if an item is in the map so the above code is too complicate for nothing. No need for a for loop. See http://www.cplusplus.com/reference/map/map/lower_bound/. You call that function and then use key_compare to verify if a match was found or the insertion point was returned.

 
Share this answer
 
Quote:
Found the solution here :
http://www.cplusplus.com/reference/map/map/operator
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900