Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Doubt in STL map

Status
Not open for further replies.

glathaa

Programmer
Aug 21, 2002
21
0
0
IN
Hi,

Is it possible to have a structure as a key in stl-map?

Let us consider the following declaration.

struct key {
int a;
int b;
};

struct compare {
bool operator()(const struct key k1, const struct key k2) const {
return ((k1->a < k2->a) && (k2->a < k2->b));
}
};

map<struct key, float, compare> mymap;

Is this declaration correct? will it work correctly?

Thanks for your response.

-Latha
 
I am unsure if that will work, but yes, the key can be any type including a struct. My understanding is that unless other wise told, the sort will use the types < operator... Granted I don't often play with maps, but simply writing a bool operator<(key,key) function should work without writting a compare.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top