codeup Speech patterns(25) map练习

 

#include <iostream>
#include <cstring>
#include<map>
#include<cmath>
using namespace std;
map<string,int>mp;
bool check(char ch){
	if(isalpha(ch)) return true; 
    //isalpha(ch)是判断ch是否是英文字母,大写返回2,小写返回1,否则返回0
if(ch>='0'&&ch<='9')return true;
    return false;

}

int main() {
	string str;
	string word;
    //将输入流的字符串放在str中,默认遇到回车停止
	getline(cin,str);
	for(int i=0;i<str.length();i++){
		if(check(str[i])){
		if(isupper(str[i]))
			ss[i]+=32;
			word+=str[i];//将ss[i]拼接到word
		} 
		if(i==str.length()-1||check(str[i])==0) //如果str[i]非英文字母或者遍历到最后一个字符
		{
			//如果单词长度不为0,则单词出现的次数加一
			if(word.length()!=0){
				mp[word]++; //mp[word]表示单词出现的次数,键是单词,值是次数
			}
			word.clear();
		}
	}
int mx=-1;
string ans;
map<string,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++) //依次遍历mp,mp会默认以键的大小由小到大排序
{
   //求出最大的值,并将单词赋给ans,次数赋给mx;如果有相等的mx,
    //取mx原先取到的单词,也就是按字典排序更小的单词
	if(it->second>mx){  
		mx=it->second;
		ans=it->first;
	}
}
	
cout<<ans<<" "<<mx;
     return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值