d*******n 发帖数: 524 | 1 The output of the following piece of simple code to test stl map is very
weird.
//-------------------code-----------------------------
#include
#include |
d*****e 发帖数: 7368 | 2 change myMap to
map myMap; |
d****p 发帖数: 685 | 3 your compare function wrong.
each key in myMap is converted to a A object with index set to 0 and thus
totally disables the compareA.
change (lhs.index < rhs.index); to lhs.str < rhs.str will populate the map
with 3 elements but not sure it is what u want. |
z****e 发帖数: 2024 | 4 try to add explicit to your constructor to see the effect.
explicit A::A(string, int) |
d*******n 发帖数: 524 | 5 Thanks but could you elaborate?
I don't see why the index is set to 0. When the key is converted?
【在 d****p 的大作中提到】 : your compare function wrong. : each key in myMap is converted to a A object with index set to 0 and thus : totally disables the compareA. : change (lhs.index < rhs.index); to lhs.str < rhs.str will populate the map : with 3 elements but not sure it is what u want.
|
z****e 发帖数: 2024 | 6 you have default argument value int=0 in your A::A(string, int);
【在 d*******n 的大作中提到】 : Thanks but could you elaborate? : I don't see why the index is set to 0. When the key is converted?
|
t****t 发帖数: 6806 | 7 First of all, the key here is string. You want to compare 2 strings in your
comparator, not 2 A objects.
But you provide a comparator that accepts 2 A objects. Therefore the 2
strings are converted to 2 A objects, with index set to 0 (you provided
default value). So any 2 strings are always equal.
You need to clear up your concepts. It's totally a mess.
【在 d*******n 的大作中提到】 : Thanks but could you elaborate? : I don't see why the index is set to 0. When the key is converted?
|
d*******n 发帖数: 524 | 8 I see......
I did mess up the logic.
So unless you want to use a self-defined or some weird thing as key, you
probably don't need to define the comparator.
your
【在 t****t 的大作中提到】 : First of all, the key here is string. You want to compare 2 strings in your : comparator, not 2 A objects. : But you provide a comparator that accepts 2 A objects. Therefore the 2 : strings are converted to 2 A objects, with index set to 0 (you provided : default value). So any 2 strings are always equal. : You need to clear up your concepts. It's totally a mess.
|
t****t 发帖数: 6806 | 9 exactly -- looks like you are comparing the integer, so why not use the
integer as index? even better, use an array?
【在 d*******n 的大作中提到】 : I see...... : I did mess up the logic. : So unless you want to use a self-defined or some weird thing as key, you : probably don't need to define the comparator. : : your
|
d*******n 发帖数: 524 | 10 here I am doing trying out some tests,
but to your question, key should be chosen by the nature of the problem not
the nature of the data-structure, right? that is, most likely you don't have
the freedom to choose what to be the key.
【在 t****t 的大作中提到】 : exactly -- looks like you are comparing the integer, so why not use the : integer as index? even better, use an array?
|
t****t 发帖数: 6806 | 11 exactly, you select the best data structure for your problem. you seem to
want to use the integer as the key since you compared it -- then you should
use array.
so make up your mind: what do you want to use as key? the string, use map<
string, int> or map; the index, use map or multimap<
int, string> or vector or string[].
you still need to clear up your concepts. it's still a mess.
not
have
【在 d*******n 的大作中提到】 : here I am doing trying out some tests, : but to your question, key should be chosen by the nature of the problem not : the nature of the data-structure, right? that is, most likely you don't have : the freedom to choose what to be the key.
|
d*******n 发帖数: 524 | 12 map is what I need and the string is used like a name (i.e. key).
The reason I was comparing index was because I though the map needs a
comparator for the value (class A), so I simply gave it one based on
comparing index, which is not really needed. This turned out to be a
misunderstanding that messed the logic up.
Other than that, nothing is messed up.
should
multimap<
【在 t****t 的大作中提到】 : exactly, you select the best data structure for your problem. you seem to : want to use the integer as the key since you compared it -- then you should : use array. : so make up your mind: what do you want to use as key? the string, use map< : string, int> or map; the index, use map or multimap< : int, string> or vector or string[]. : you still need to clear up your concepts. it's still a mess. : : not : have
|