Re: std::map with nonunique keys
On 4 Set, 11:31, Francesco <entul...@gmail.com> wrote:
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]) r=
eturn 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 differen=
t
in k[9] there evaluation only depends on k[0]=85k[8]. If I understand i=
t
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.
Self correction: "...then any operation on the map using either of
those keys will overwrite *each other's value*...".
The key will be saved only the first time it is used, and won't be
modified by subsequent lookups - at least, my implementation works
this way, but I suppose it's the mandated behavior.
Code:
-------
#include <iostream>
#include <map>
struct foo {
int used;
int not_used;
foo(int u = 0, int nu = 0) : used(u), not_used(nu) {};
bool operator<(const foo& f) const {
return used < f.used;
}
};
using namespace std;
int main() {
map<foo, int> m;
foo f1(1,1);
foo f2(1,2);
m[f1] = 1;
cout << "m.begin()->first.not_used == ";
cout << m.begin()->first.not_used << endl;
cout << "m[f1] == ";
cout << m[f1] << endl;
cout << "m[f2] = 2" << endl;
m[f2] = 2;
cout << "m.begin()->first.not_used == ";
cout << m.begin()->first.not_used << endl;
cout << "m[f1] == ";
cout << m[f1] << endl;
return 0;
}
-------
Output:
-------
m.begin()->first.not_used == 1
m[f1] == 1
m[f2] = 2
m.begin()->first.not_used == 1
m[f1] == 2
-------
Cheers,
Francesco