在一段文本中搜索并打印出包含某个单词的句子,根据出现次数排序

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">  #include <map></span>
 #include <iostream>
 #include <functional>  //因为用了greater<int>()
// #include <utility>---包含最基本的STL
using namespace std;

<pre name="code" class="cpp">不区分大小写

 
//先统计一句话出现的单词的次数
 int match(const char *s, const char *p = "your")
 {
         if(s == NULL || s == "")
                 return 0;

         int i = 0, j = 0;
         int count = 0;
         while(s[i] != '\0'){
                 if(s[i] == p[j] || s[i] == p[j] - 32 )
                         ++j;
                 else
                         j = 0;

                 if(p[j] == '\0'){
                         ++count;
                         j = 0;
                 }
                 ++i;
         }

         return count;
 }

//再利用新的map存放
 int main()
 {
         char* array[] = {"Make yourself at home"
                 ,"None of your business"
                 ,"I will be more careful"
                 ,"How about going to a move?"
                 ,"Your life is your own affair"
                 ,0};

         typedef multimap<int,char*,greater<int>> mymap;  //greater<int>为内置比较函数
         mymap mp;

         for(int i = 0; array[i] != 0; ++i){
                 mp.insert(make_pair(match(array[i]),array[i]));
         }

         
         for(mymap::iterator it = mp.begin(); it != mp.end(); ++it){
                 if(it->first != 0)
                         cout<<it->first<<"   "<<it->second<<'\n';
         }
         cout<<endl;
         
         return 0;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值