Re: std::map with nonunique keys
On 4 Set, 11:20, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.de> wrote:
Hi,
ist it possible to have a map for which there are distinct keys k1 and
key with k1!=k2 and both k1<k2 and k2<k1 returning false? I'd like to
have something like
struct foo {
int k[10];
bool operator<(const foo& rhs) const {
for (int i=0;i<9;++i)
if (k[i]!=rhs.k[i]) ret=
urn k[i]<rhs.k[i];
return false;
}
};
std::map<foo,int> evaluation_map;
The reason is that although two variables of type foo might be different
in k[9] there evaluation only depends on k[0]=85k[8]. If I understand it
correctly, a map never uses operator==, therefore the above shouldn't
produce undefined behaviour, right?
If your operator<(), applied twice, swapped, to a couple of keys,
finds them to be equal, then any operation on the map using either of
those keys will overwrite each other - that is, well defined behavior.
You - the implementer of the struct foo - know that two keys can be
physically different, but the map will find them logically equal and
will work on that assumption.
If you want multiple logically equivalent keys into a map you have to
use a multimap.
Hope that helps,
Francesco