题目描述:
解题思路:
- 遍历序列,使用map存储
代码实现:
#include <iostream>
#include <map>
#include <string>
using namespace std;
void RemoveSpace(string &s) {
string ans = "";
for (auto & c : s) {
if (c != ' ') {
ans += c;
}
}
s = ans;
}
int main() {
string s;
cout << "please input a str:" << endl;
getline(cin, s);
RemoveSpace(s);
std::map<int, int> mp;
for (auto c: s) {
if (isdigit(c)) {
cout << "c = " << c << endl;
mp[c - '0']++;
}
}
for (auto x : mp) {
cout << x.first << " :" << x.second << endl;
}
return 0;
}