开发者

std::map custom key non-uniqueness problem

Couldn't find an answer on Google..

When I use std::string the following works fine.

map <fxString, int> test;
test.insert(pair <fxString, int> ("Bla", 1));
test.insert(pair <fxString, int> ("Bla", 2));
test.insert(pair <fxString, int> ("Bla", 3));
cout << fxInt2String(test["Bla"]) << endl;

Which should output 1, but instead outputs 0

When I iterate through the map every key value pair is there, sitting right next to each other mocking me.

fx开发者_JS百科String defines the following operators: operator > operator < operator == operator !=

And more, and I tested them..

Grom.


"When I iterate through the map every key value pair is there"

Well, something is wrong with your fxString::operator< then, because the insert member function should have no effect if the key is already present. Are you sure this operator models a strict weak ordering ?

Assuming that the operator is broken, test["Bla"] has the effect of adding yet another element in the map with a default value of 0.


This works fine (as you have noted)

map <string, int> test;
test.insert(pair <string, int> ("Bla", 1));
test.insert(pair <string, int> ("Bla", 2));
test.insert(pair <string, int> ("Bla", 3));
cout << test["Bla"] << endl;

So there must be either problem with your fxInt2String function or with your operator overloads.

To receive further help you should provide us with code for the functions in question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜