您的位置:首页 > 其它

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

2014-10-06 19:19 281 查看
<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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐