关于map按照value排序的问题。

xiaoZhang888 2009-04-22 08:15:47
请问大家map如何按照value来进行排序,试了一些,没有试出来。谢谢各位了。
...全文
1564 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
川哥119 2009-09-14
  • 打赏
  • 举报
回复
自定义一个比较器就可以实现:具体代码如下:
/**********自定义一个比较器*************/
static class EntryComparator implements Comparator<Map.Entry<String, Float>> {
public EntryComparatorAAA() {}
public int compare(Map.Entry<String, Float> o1, Map.Entry<String, Float> o2) {
// return o1.getValue().intValue() - o2.getValue().intValue(); //升序排列
return o2.getValue().intValue() -o1.getValue().intValue();//降序排列
}
}

/*******下面是具体的排序方法*******/

@SuppressWarnings("unchecked")
public static Map<String, Float> outputSortedHashMap(HashMap<String, Float> map) {
Map<String, Float> tempMap = new LinkedHashMap<String, Float>();
List<Map.Entry<String, Float>> list = new ArrayList<Map.Entry<String, Float>>(map.entrySet());
Collections.sort(list,new EntryComparator());
Iterator<Map.Entry<String, Float>> i = list.iterator();
while (i.hasNext()) {
Map.Entry<String, Float> entry = i.next();
tempMap.put(entry.getKey(),entry.getValue());
System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
}
return tempMap;
}
  • 打赏
  • 举报
回复
map本质是一个红黑树....为什么LZ一定要用map排序呢???
用些vector,list,不是更好??再不行就按照ls说的吧
sytstarac 2009-04-23
  • 打赏
  • 举报
回复
还有key是key,mapped_value,pair<key,mapped_value>才是value type
sytstarac 2009-04-23
  • 打赏
  • 举报
回复
map的构造参数有个默认比较函数对象的参数:class Cmp=less<Key>,即默认按照Key的<运算符来排序。你可以提供一个比较函数对象,来定义自己的排序规则。
lpf000 2009-04-22
  • 打赏
  • 举报
回复
map 就是按键类型的<操作符 排序的;
你要按键关联的值类型排序,再创建一个容器,把值变成键。
  • 打赏
  • 举报
回复
http://blog.csdn.net/e3002/archive/2008/04/09/2270640.aspx

map实现排序功能

fly_feng 2009-04-22
  • 打赏
  • 举报
回复
map是关联容器,是已序的。
donle1000 2009-04-22
  • 打赏
  • 举报
回复
说实话...这程序写的太恶心了
自增自减用的很混乱.

#pragma warning(disable: 4786)
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
map <string ,int> m;
map <string, int> n_m;
string s;

while(cin >> s)
{
++m[s];
cin.sync();
cin.clear();
}

for(map <string, int>::iterator it = m.begin(); it != m.end(); )
{
if(it-> second > (++it)-> second)
{
if(it == m.end())
break;
n_m.insert(pair <string, int> (it-> first, it-> second));
++it;
n_m.insert(pair <string, int> (it-> first, it-> second));
}
else
{
--it;
n_m.insert(pair <string, int> (it-> first, it-> second));
++it;
n_m.insert(pair <string, int> (it-> first, it-> second));
}
}

for(map <string, int>::iterator n_it = n_m.begin(); n_it != n_m.end(); ++n_it)
{
cout << n_it-> first << '\t' << n_it-> second << '\n';
}

return 0;
}
  • 打赏
  • 举报
回复
先告诉你,map本身是按照升序排列的。

如果不需要逆序,不需要对map排序的。
Cpp权哥 2009-04-22
  • 打赏
  • 举报
回复
只要你把数据放到map中就会自动按key排序,不会按value排序。建议你新建一个map,把原来的key与value的类型对调,把原来map中的每个元素取出来把key跟value对调形成新的元素插入到新map中。
onlinewan 2009-04-22
  • 打赏
  • 举报
回复
你为什么要用map做这样的事情?
是不是设计有不妥?
liliangbao 2009-04-22
  • 打赏
  • 举报
回复
帮顶先!
sherrik 2009-04-22
  • 打赏
  • 举报
回复
一个一个遍历map 然后交换两个值 插入新的map里
没试过 不知道行不行呵呵
baiwei156 2009-04-22
  • 打赏
  • 举报
回复
map反正是按key来自动升序排列的。。。

想用value来排序,那就用排序算法来输出。。。。

或者反转key和value。。。。

xiaoZhang888 2009-04-22
  • 打赏
  • 举报
回复
int main()
{
map<string ,int> m;
map<string, int> n_m;
string s;

while(cin >> s)
{
++m[s];
}


for(map<string, int>::iterator it = m.begin(); it != m.end(); )
{
if(it-> second > (++it) -> second)
{
n_m.insert(pair<string, int> (it-> first, it-> second));
++it;
}
else
{
--it;
n_m.insert(pair<string, int> ((++it)-> first, (++it)-> second));
++it;
}
}

for(map<string, int>::iterator n_it = n_m.begin(); n_it!= n_m.end(); ++n_it)
{
cout << n_it-> first << '\t' << n_it-> second << '\n';
}

return 0;
}
这个是我写的,边界有问题,也不知道怎么弄,请大家帮我看一下~
mengde007 2009-04-22
  • 打赏
  • 举报
回复
map的底层是RB_TREE;楼主可以去看看STL;
zgjxwl 2009-04-22
  • 打赏
  • 举报
回复
你自己先把你写的放上来吧。。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧