Redian新闻
>
What's problem with this piece of code using stl map?
avatar
z*n
2
护照公证需要填表吗?还是公证员直接在护照复印件上敲戳签名就行了?
多谢了。
avatar
a*t
3
还是一定要找个朋友做translator?还有certification里文件名称写中文名还是英文名
称?
avatar
d*n
4
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
avatar
M*7
5
micro sd
avatar
w*g
6
certified copy is enough

【在 z********n 的大作中提到】
: 护照公证需要填表吗?还是公证员直接在护照复印件上敲戳签名就行了?
: 多谢了。

avatar
f*u
7
可以自己翻译,然后找人证明你翻译的对。
avatar
d*e
8
change myMap to
map myMap;
avatar
a*t
9
就是在那个证明里面,是要填朋友的名字,不能填自己的?

【在 f*********u 的大作中提到】
: 可以自己翻译,然后找人证明你翻译的对。
avatar
d*p
10
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.
avatar
z*e
11
try to add explicit to your constructor to see the effect.
explicit A::A(string, int)
avatar
d*n
12
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.

avatar
z*e
13
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?

avatar
t*t
14
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?

avatar
d*n
15
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.

avatar
t*t
16
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

avatar
d*n
17
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?

avatar
t*t
18
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.

avatar
d*n
19
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。