输入字符串,输出各个字符的个数(map用法小例)

#include
#include
#include

using namespace std;

void main()
{
string str;
getline(cin, str);
map mapChar;
for (int i = 0; i < str.size(); ++i)
{
++mapChar[str[i]];
}
for (map::const_iterator  it = mapChar.begin(); it != mapChar.end(); ++it)
{
cout << it->first << "    " << it->second << endl;
}

}

你可能感兴趣的:(C/C++)