stl 中怎样遍历一个map中的所有元素。请给是实例

vlmf 2002-03-09 06:09:15
stl 中怎样遍历一个map中的所有元素。请给是实例
...全文
7254 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangrk 2002-03-11
  • 打赏
  • 举报
回复
怎么不给分?
zheng_can 2002-03-09
  • 打赏
  • 举报
回复
用 map<>::iterator
fangrk 2002-03-09
  • 打赏
  • 举报
回复
http://www.csdn.net/expert/topic/552/552836.xml?temp=.4185602
“我想写一个参数为字符串,返回类型为char的一个函数,其作用是返回字符串中出现频率最多的一个字符,请高手给出实现代码,非常非常非常感谢!!!!”

//bcc 5
#include <map>
#include <iostream>
using namespace std;
char maxCount(const char *);

void main()
{ char buff[200];
cout<<"Please input string:"<<endl;
cin>>buff;
cout<<"The max count char in buff is:"<<maxCount(buff)<<endl;
}
char maxCount(const char * string)
{ map<char,int> c_map;
const char *p=string;
while(*p){
c_map[*p]++;
p++;
}
int max=0;
map<char,int>::iterator it=c_map.begin();
char find=it->first;
for(;it!=c_map.end();it++){
if(max<it->second){
max=it->second;
find=it->first;
}
}
return find;
}
hhdsq 2002-03-09
  • 打赏
  • 举报
回复
当然,words这个map里面首先得有元素。。
hhdsq 2002-03-09
  • 打赏
  • 举报
回复
从essential c++上搬来的,还没验证:

#include<map>
#include<string>
#include<iostream>

int main()
{
map<string,int> words;
map<string,int>::iterator it=words.begin();
for(;it!=words.end();++it)
cout<<"key:"<<it->first
<<"value:"<<it->second<<end1;
return 0;
}

69,378

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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