读书人

用C++实现输入一段文字后查询某个单词

发布时间: 2012-04-02 19:58:59 作者: rapoo

用C++实现输入一段文字后查询某个单词出现的次数
求给我一个完整程序,我水平不行,C++学的比较基础

[解决办法]

C/C++ code
#include <iostream>#include <list>#include <string>#include <fstream>#include <algorithm>using namespace std;int MatchString( const string& str){    string s("this") ;    return (s == str);}int main(){    ifstream ifs("MyLog1.log");    char c;    string s;    list<string> ls;    s.clear();    if (!ifs.bad())    {        //Print the contents in the file.        cout << ifs.rdbuf();        cout << endl;        //this is what this is what        do         {            ifs.get(c);            if (c != ' ')            {                s.append(1, c);            }            else if(s != ""){                ls.push_back(s);                s.clear();            }        } while (!ifs.eof());        ifs.close();    }    //Print the result.     cout << count_if(ls.begin(), ls.end(), MatchString) << endl;    //2    system("pause");    return 0;}
[解决办法]
你可以自己实现一个 Map,单词做键,次数做值,在将这段文本分解为单词,加入该Map,结果就出来了。

读书人网 >C++

热点推荐