Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

STL map ( no unique element )

Hi there,

Are there any experts out there ?

I know STL map only allow unique key but how about its element ?
I want the element to be unique too. Is there an easy way to do it ?

I can compare every element in the map and make sure it's unique but i am posting here for a better way.

eg grade_list["john"] = 'A';

The 'A' is the element and i want it to be unique for all element in the map. There's a reason for what i am doing.

I don't want to create additional set structure.

Please help.

cheers
[607 byte] By [overule] at [2007-11-11 10:28:06]
# 1 Re: STL map ( no unique element )
Either you can write a wrapper or try some customized STL (i guess few are available as part of ACE)
it career at 2007-11-11 20:58:51 >
# 2 Re: STL map ( no unique element )
It seems like i have to compare every element in the map.

so is this efficient enough ?

for(iter = mymap.begin(); iter != mymap.end(); iter++)
{
cout << endl;

for(iter1 = mymap.begin(); iter1 != mymap.end(); iter1++)
{
if(*iter != *iter1)
{
// if two elements are equal, then print out "Equal"
if()
cout << " iter: " << iter->second << " iter1: " << iter1->second << endl;
}
}

}

If not, please suggest a better solution.

thanks
overule at 2007-11-11 20:59:52 >
# 3 Re: STL map ( no unique element )
I would use a different approach: instead of storing the name as the key, use the grade as the associated value (what you call "the element"). Alternatively, use the unique(), remove_if() etc. algorithms to ensure uniqueness of the map's elements.
Danny at 2007-11-11 21:00:47 >