HJ102 字符统计 C++实现

#include <iostream>

#include <string>

#include <map>

#include <vector>

#include <algorithm>

using namespace std;

int main() 

{

    string str = "";

    map<charint> strCountMap = {};

    while (cin >> str)  // 注意 while 处理多个 case

    {

        for (size_t i = 0; i < str.size(); i++) {

            auto iter = strCountMap.find(str[i]);

            if (iter == strCountMap.end()) {

                strCountMap.insert(std::pair<char,int>(str[i], 1));

            } else {

                iter->second += 1;

            }

        }

        // 对map中的元素按照个数进行降序排序

        std::vector<std::pair<charint>> resultVec(strCountMap.begin(), strCountMap.end());

        std::sort(resultVec.begin(), resultVec.end(),

            [](const std::pair<charintleft ,const std::pair<charintright) {

                if (left.second == right.second) {

                    return left.first < right.first;

                }

                return left.second > right.second;

        });

        for (auto iter : resultVec) {

            cout << iter.first;

        }

        cout << std::endl;

    }

    return 0;

}

全部评论
最近要开始刷题了。。。
点赞 回复 分享
发布于 2022-12-09 00:13 美国

相关推荐

06-12 16:50
已编辑
小米_软件开发(准入职员工)
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务