map>::iterator i;和map>m;

z75050429 2008-10-06 08:32:32

#include <map>
#include <iostream>
using namespace std;
int main(void)
{
map<char,int,greater<int>>m;
//map<int,int>m;
m['a']=1;
m['b']=3;
m['c']=19;
m['d']=15;
m['e']=10;
map<char,int,greater<int>>::iterator i;
//map<int,int>::iterator i;
for (i= m.begin();i!=m.end();i++)
{
cout<<(*i).first<<' '<<(*i).second<<endl;
}
return 0;
}其中的greater<int>如果该成greater<char>逗可以运行为什么?意义是什么?
...全文
827 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
MagiSu 2008-10-07
  • 打赏
  • 举报
回复
同楼上的

C++是强类型的,特别是模板也是强类型的。

map<K,T,P,A>中K需要有比较,来排序。你给的是T的greater
ysuliu 2008-10-07
  • 打赏
  • 举报
回复
结帖率:0.00%

应该是用char,可是你用int也不会出错,编译器会自动转换。
Fighting Horse 2008-10-07
  • 打赏
  • 举报
回复
greater <int>,作为一个函数类型,bool (*)(const int & l, const int & r),可以接受char类型的参数,编译器自动转换类型,而且计算的结果也是正确的,所以排序仍然正常
z75050429 2008-10-07
  • 打赏
  • 举报
回复
我将他改成int 结果还是在进行K的排序,你不信可以试试·
chlaws 2008-10-06
  • 打赏
  • 举报
回复
template<class Key, class T, class Pred = less<Key>, class A = allocator<T> >
class map{..};
类型要一致啊, 你指定的key类型是char 所以你用的函数对象的类型也要是char.
看看这原型就该明白了吧

65,179

社区成员

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

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