牛客网 单词识别题解

题目描述

输入一个英文句子,把句子中的单词(不区分大小写)按出现次数按从多到少把单词和次数在屏幕上输出来,要求能识别英文句号和逗号,即是说单词由空格、句号和逗号隔开。
 
我的题解:
#include <iostream>
#include <algorithm>
#include <sstream>
#include <map>
using namespace std;

class project{
public:
    void fun1(){
        string line,word;
        map<string, size_t> word_count;
        getline(cin,line);
        transform(line.begin(), line.end(), line.begin(), ::tolower);
        for(auto i=line.begin(); i!=line.end(); i++){
            if(*i==',' || *i=='.') *i=' ';
        }
        stringstream s(line);
        while(s >> word)
            ++word_count[word];
        for(const auto &w : word_count)
            cout << w.first << ":" << w.second << endl;
    }
    
};

int main()    
{
    project P;
    P.fun1();
    return 0;
}

 新知识点:

transform(line.begin(), line.end(), line.begin(), ::tolower);//<algorithm> 将string中大写转换为小写。

 

转载于:https://www.cnblogs.com/HonkerYblogs/p/10548749.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值