Re: std::map
Ashish wrote:
using std::string as key in map i dont need to overload any extra
functionality. map will be like
std::map<std::string, data>
But using const char* i need to add the functionality for key_compare and
allocator_type.
std::map<const char*,data,functionpointer,allocator> mapdata;
where functionpointer contains pointer to function which compare both keys
and allocator will allocate memory for key.
Because if i insert item in map like
mapdata.insert(std::make_pair("abc",data)); then
mapdata.find("abc") will return that item.
while if i insert item like
std::string str = "abc";
mapdata.insert(std::make_pair(str.c-str(),data)); then
mapdata.find("abc") will not return that item.
Since i cant save all keys in some class so i need to allocate memory for
each key in map itself.
Please suggest how to allocate memory for char* in map. If allocator_type
does this job then please give any sample how?
Anish:
If you write your comparison function to compare the strings, then both your
uses of std::map::find() will find the key "abc".
David Wilkinson
Visual C++ MVP