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 美国

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
萧索X:写篮球联赛干嘛,陪老板打篮球吗。还有实习经历要写自己所在岗位具体完成什么工作,自己的任务具体完成了什么需求,给公司带来了哪些量化增长
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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