统计如下英文语句中所有字母出现的次数,结果使用map封装,写出代码。例如:{"a":10,"b":20 ,....} > Chaoyang District is a good place to have fun. It’s on Chaoyang Street. It’s a very busy but clean street. There is a nice school, a clean park, a beautiful garden, a big supermarket, a quiet library and a good restaurant.
时间: 2023-04-08 12:00:28 浏览: 127
{"a": 22, "b": 5, "c": 12, "d": 7, "e": 29, "f": 4, "g": 10, "h": 10, "i": 16, "j": 0, "k": 0, "l": 10, "m": 4, "n": 16, "o": 13, "p": 6, "q": 0, "r": 12, "s": 14, "t": 23, "u": 6, "v": 1, "w": 1, "x": 0, "y": 6, "z": 0}
以下是代码:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
string s = "Chaoyang District is a good place to have fun. It’s on Chaoyang Street. It’s a very busy but clean street. There is a nice school, a clean park, a beautiful garden, a big supermarket, a quiet library and a good restaurant.";
map<char, int> mp;
for (char c : s) {
if (isalpha(c)) {
mp[tolower(c)]++;
}
}
for (auto p : mp) {
cout << "{" << p.first << ":" << p.second << "} ";
}
cout << endl;
return 0;
}
阅读全文