map排序问题

liyanlin122 2010-04-11 11:26:53

#include <iostream>
#include <algorithm>
#include <map>
#include <functional>
using namespace std;

bool less(const float & f1, const float & f2)
{
return (f1<f2);
}

int main()
{
map<float,int> m;
int n, i = 1;
float score;
cin >> n;

while(i <= n)
{
cin >> score;
m[score] = i;
i++;
}

sort(m.begin(),m.end(),less);
map<float, int>::iterator iter = m.begin();
while(iter != m.end())
{
cout << iter->first << "\t\t" << iter->second << endl;
iter++;
}

return 0;
}




目的:按map中的score排序
错误:
C:\Documents and Settings\l\桌面\下载\C++\c.cpp(7) : error C2955: 'less' : use of class template requires template argument list
c:\program files\microsoft visual studio\vc98\include\functional(87) : see declaration of 'less'
C:\Documents and Settings\l\桌面\下载\C++\c.cpp(26) : error C2872: 'less' : ambiguous symbol

求教高手~~~~
...全文
98 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
caidonghen 2010-04-12
  • 打赏
  • 举报
回复
正在学习
liyanlin122 2010-04-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 loaden 的回复:]
另外,map本身就已经针对key排序了。不知道你的用意是啥?
[/Quote]
要按map中的第一个参数来排序!
wang7890 2010-04-12
  • 打赏
  • 举报
回复
map本身就已经针对key排序了
delphiwcdj 2010-04-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 loaden 的回复:]

另外,map本身就已经针对key排序了。不知道你的用意是啥?
[/Quote]
是啊,map的key是隐式按字典序排列的
kingstarer 2010-04-11
  • 打赏
  • 举报
回复
对map也能sort啊 以前还不知道呢
老邓 2010-04-11
  • 打赏
  • 举报
回复
另外,map本身就已经针对key排序了。不知道你的用意是啥?
老邓 2010-04-11
  • 打赏
  • 举报
回复
把less改个名,名字冲突了。

64,654

社区成员

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

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