题解 | #字母统计#
字母统计
https://www.nowcoder.com/practice/de7bf0945c1c4bd1aa9d49573b831f3c
#include<bits/stdc++.h> using namespace std; int main() { string str; getline(cin, str); map<char, int> count = { //输入的序列中不一定包含所有字母,所以要将所有字母都输入map中 {'A', 0}, {'B', 0}, {'C', 0}, {'D', 0}, {'E', 0}, {'F', 0}, {'G', 0}, {'H', 0}, {'I', 0}, {'J', 0}, {'K', 0}, {'L', 0}, {'M', 0}, {'N', 0}, {'O', 0}, {'P', 0}, {'Q', 0}, {'R', 0}, {'S', 0}, {'T', 0}, {'U', 0}, {'V', 0}, {'W', 0}, {'X', 0}, {'Y', 0}, {'Z', 0} }; //遍历输入的字符串统计各个字母出现的次数 for (char c : str) { if (isalpha(c)) { count[c]++; } } //将统计结果输出 for (auto iter = count.begin(); iter != count.end(); iter++) { cout << iter->first << ":" << iter->second << endl; } }