由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - What's problem with this piece of code using stl map?
相关主题
为啥gcc找不到类的构造函数?c++小问题
C++问题,confusing...一个关于C++ template和overload的问题
为什么在overloading中,friend <<不能读取private值呢?conversion between const to nonconst
[合集] 关于template和inheritance的问题请教编译器如何分辨返回类型不同的函数?
Why should i include .cpp instead of .hnamespace 问题
一个关于assignment constructor和expection的问题请问这个C++程序有什么问题吗
[合集] 怎样有效的传递C静态数组的变量名?为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?
问个 std::vector 的基本问题请教各路C++大神 为什么f(3) 输出是 'dd'
相关话题的讨论汇总
话题: string话题: map话题: index话题: int话题: what
进入Programming版参与讨论
1 (共1页)
d*******n
发帖数: 524
1
The output of the following piece of simple code to test stl map is very
weird.
//-------------------code-----------------------------
#include
#include
using namespace std;
class A{
public:
class CompA{
public:
bool operator() (const A& lhs, const A& rhs) const {
return (lhs.index < rhs.index);
}
};
string str;
int index;
A(string s = "", int i = 0) : str(s), index(i) {}
};
int main(){
map myMap;
m
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

1 (共1页)
进入Programming版参与讨论
相关主题
请教各路C++大神 为什么f(3) 输出是 'dd'Why should i include .cpp instead of .h
一个C++ 的问题一个关于assignment constructor和expection的问题
Question about [合集] 怎样有效的传递C静态数组的变量名?
compare double to float问个 std::vector 的基本问题
为啥gcc找不到类的构造函数?c++小问题
C++问题,confusing...一个关于C++ template和overload的问题
为什么在overloading中,friend <<不能读取private值呢?conversion between const to nonconst
[合集] 关于template和inheritance的问题请教编译器如何分辨返回类型不同的函数?
相关话题的讨论汇总
话题: string话题: map话题: index话题: int话题: what