西安交通大学915-2018-编程1

2022-02-24 19:51:14 浏览数 (1)

题目描述:

解题思路:

  • 遍历序列,使用map存储

代码实现:

代码语言:javascript复制
#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;
}

0 人点赞