爱嘉牛LA

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

以下程序是在一本C++书出现的,觉得挺好的,就保留下来。

 

#include<iostream>
#include<map>
#include<cctype>
using namespace std;
int main(){
    map<char,int>s;  //用来存储字母出现次数的映射 
    char c; //存储输入字符 
    do{
        cin>>c;//输入下一个字符 
        if(isalpha(c)){//判断是否为字母 
            c=tolower(c);//将字母转为为小写 
            s[c]++;//将该字母的出现频率加1 
        }
    }while(c!='.');
    //输出每个字母出现的次数
    for(map<char,int>::iterator iter=s.begin();iter!=s.end();++iter)
       cout<<iter->first<<" "<<iter->second<<" "<<endl;
    cout<<endl;
    return 0; 
} 
View Code

 

posted on 2014-02-20 22:07  爱嘉牛LA  阅读(593)  评论(0编辑  收藏  举报