问题:使用map统计一个字符串中的每个字符出现的次数
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
map<char, int>needs;
string s;
s = { "asdwafsada" };
for (auto x : s)
{
needs[x]++; //遍历每个字符
}
map<char, int>::iterator it; //创建一个迭代器
for (it = needs.begin(); it != needs.end(); it++) // 这里能不能使用auto这种进行遍历???
{
cout << it->first <<'='<< it->second << endl;
}
return 0;
}
输出结果: