题解 | #统计字符串中各字母字符对应的个数#

统计字符串中各字母字符对应的个数

http://www.nowcoder.com/practice/ec2a5ab818be4851948d5b0d83a3d8f4

注意事项:map<char,int> mp; map的查找:mp.find(ch) ==mp.end()表示没找到,其他表示找到 map的插入:mp.insert(pair<char,int> m) map的输出:for(auto m:mp) m.first、m.second

// write your code here......
#include<map>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    map<char,int> mp;
    for(int i=0;str[i]!='\0';i++)
    {
        if(isalpha(str[i]))
        {
            if(mp.find(str[i])==mp.end())
                mp.insert(pair<char,int>(str[i],1));
            else
                mp[str[i]]++;
        }
    }
    for(auto m:mp)
        cout<<m.first<<":"<<m.second<<endl;

    return 0;
}
全部评论

相关推荐

Java面试先知:我也是和你一样的情况,hr 说等开奖就行了
点赞 评论 收藏
分享
迷茫的大四🐶:价格这么低都能满了?
点赞 评论 收藏
分享
评论
4
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务