Redian新闻
>
How to sort a map in C++ STL based on Value, instead of Key
avatar
How to sort a map in C++ STL based on Value, instead of Key# Programming - 葵花宝典
i*e
1
map index_of_card; // ????
I want to sort the index (int) numerically. For example,
index_of_card.begin()->second will print out the samllest value,
index_of_card.end() ->second will print out the largest value, etc.
Thanks!
avatar
c*s
2
为什么不用
map

【在 i******e 的大作中提到】
: map index_of_card; // ????
: I want to sort the index (int) numerically. For example,
: index_of_card.begin()->second will print out the samllest value,
: index_of_card.end() ->second will print out the largest value, etc.
: Thanks!

avatar
i*e
3
I need map. For the time being, I have to use both
map and map.
I have thought map in C++ has the similiar way to be manipulated as
hash in Perl, where we can sort by key or by value.

【在 c****s 的大作中提到】
: 为什么不用
: map

avatar
p*f
4

you can sort c++ map by value if you want, but this would not be the
same sort carried out by the map container. I believe hash sort by
value in perl is the same case, ie. you just pass all the values to a
sort function.

【在 i******e 的大作中提到】
: I need map. For the time being, I have to use both
: map and map.
: I have thought map in C++ has the similiar way to be manipulated as
: hash in Perl, where we can sort by key or by value.

avatar
vi
5

I don't think this is doable without adding signaficant works.
You can't sort by value; and if you do, that's multi-key,
instead of key-value.

【在 i******e 的大作中提到】
: map index_of_card; // ????
: I want to sort the index (int) numerically. For example,
: index_of_card.begin()->second will print out the samllest value,
: index_of_card.end() ->second will print out the largest value, etc.
: Thanks!

avatar
i*e
6
oh, I feel good then. I have been afraid that I implemented sth which was al
ready there.

【在 vi 的大作中提到】
:
: I don't think this is doable without adding signaficant works.
: You can't sort by value; and if you do, that's multi-key,
: instead of key-value.

avatar
p*g
7
just pass a compare function or function object to sort,
your compare function(object) compares
the value instead of the key of your map.

【在 i******e 的大作中提到】
: map index_of_card; // ????
: I want to sort the index (int) numerically. For example,
: index_of_card.begin()->second will print out the samllest value,
: index_of_card.end() ->second will print out the largest value, etc.
: Thanks!

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