PDA

View Full Version : map(stl) in c++



rajveer
22nd January 2009, 08:44
hi

i have declared a structure having members as string and int.
now how can i make a function for comparision so that i used it in map.
map<key,value,function>

in function field i need user define function which takes one argument as string and other as int.


please help me out.

thanks

jacek
22nd January 2009, 20:28
Could you post the exact definition of your map? The key comparison function takes two keys as parameters, not values.

Tavo92
2nd February 2009, 12:40
You can:

1) If your key is a structure you have created you can create the comparison function like this:


bool operator<(const YourType& k0, const YourType& k1)
{
//do stuff...
//return true if k0 is LESS than k1, if k0 is equal or more than k1 return false
}

2) Pass a compare function when you create the map. Read this (http://cplusplus.com/reference/stl/map/) it will explained it very well.