• 统计文章内各个单词出现的次数


    算法的思路是:

    1. 从头到尾遍历文件,从文件中读取遍历到的每一个单词。
    2. 把遍历到的单词放到hash_map中,并统计这个单词出现的次数。
    3. 遍历hash_map,将遍历到的单词的出现次数放到优先级队列中。
    4. 当优先级队列的元素个数超过k个时就把元素级别最低的那个元素从队列中取出,这样始终保持队列的元素是k个。
    5. 遍历完hash_map,则队列中就剩下了出现次数最多的那k个元素。

      具体实现和结果如下:

    // 出现次数最多的K个单词.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    #include <hash_map>
    #include <string>
    #include <fstream>
    #include <queue>
    #include <iostream>
    #include <algorithm>
    #include <boost/timer.hpp> 
    using namespace std;
    using namespace boost;
    void top_k_words()//出现次数最多的是个单词
    {
        timer t;
        ifstream fin;
        fin.open("modern c.txt");
        if (!fin)
        {
            cout<<"can not open file"<<endl;
        }
        string s;
        hash_map<string,int> countwords;
        while (true)
        {
            fin>>s;
            countwords[s]++;
            if (fin.eof())
            {
                break;
            }
            
        }
        cout<<"单词总数 (重复的不计数):"<<countwords.size()<<endl;
        priority_queue<pair<int,string>,vector<pair<int,string>>,greater<pair<int,string>>> countmax;
        for(hash_map<string,int>::const_iterator i=countwords.begin();
            i!=countwords.end();i++)
        {
            countmax.push(make_pair(i->second,i->first));
            if (countmax.size()>10)
            {
                countmax.pop();
            }
        }
        while(!countmax.empty())
        {
            cout<<countmax.top().second<<" "<<countmax.top().first<<endl;
            countmax.pop();
        }
        cout<<"time elapsed "<<t.elapsed()<<endl;
    }
    int main(int argc, char* argv[])
    {
        top_k_words();
    
        system("pause");
        return 0;
    }

    linux下不能使用hash_map,改为map来统计单词的个数:

    // 出现次数最多的K个单词.cpp : Defines the entry point for the console application.  
    #include <map>
    #include <string>
    #include <fstream>
    #include <queue>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    void top_k_words()//出现次数最多的是个单词  
    {
        ifstream fin;
        fin.open("modern c.txt");
        if (!fin)
        {
            cout<<"can not open file"<<endl;
        }
        string s;
        map<string,int> countwords;
        while (true)
        {
            fin>>s;
            countwords[s]++;
            if (fin.eof())
            {
                break;
            }
    
        }
        cout<<"单词总数 (重复的不计数):"<<countwords.size()<<endl;
        priority_queue<pair<int,string>,vector<pair<int,string>>,greater<pair<int,string>>> countmax;
        for(map<string,int>::const_iterator i=countwords.begin();  i!=countwords.end();i++)
        {
            countmax.push(make_pair(i->second,i->first));
            if (countmax.size()>10)
            {
                countmax.pop();
            }
        }
        while(!countmax.empty())
        {
            cout<<countmax.top().second<<" "<<countmax.top().first<<endl;
            countmax.pop();
        }
    }
    int main(int argc, char* argv[])
    {
        top_k_words();
    
        return 0;
    }
  • 相关阅读:
    C语言寒假大作战01
    C语言|博客作业12-学期总结
    第一次作业
    C语言|博客作业11
    C语言|博客作业10
    Centos7上安装 sqlmap 所经历的坑
    软件工程1916|W(福州大学)_助教博客】助教总结
    软件工程1916|W(福州大学)_助教博客】个人总结作业(第12次)成绩公示
    团队作业第五次(第8次)—项目系统设计与数据库设计成绩排名
    需求分析课堂答辩问题汇总
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4389430.html
Copyright © 2020-2023  润新知