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

zyx1009 2012-03-02 07:09:13
求给我一个完整程序,我水平不行,C++学的比较基础
...全文
904 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
allIdover 2012-03-03
  • 打赏
  • 举报
回复
如果你懒的话。。。你可以使用标准模板库中的Maps
allIdover 2012-03-03
  • 打赏
  • 举报
回复
你可以自己实现一个 Map,单词做键,次数做值,在将这段文本分解为单词,加入该Map,结果就出来了。
Binzo 2012-03-02
  • 打赏
  • 举报
回复
上面的程序有点问题。
忘了注释掉一些语句了。
#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;
}
Binzo 2012-03-02
  • 打赏
  • 举报
回复

#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;
}
篾匠 2012-03-02
  • 打赏
  • 举报
回复
C++ Primer里有这个例子吧
一aa一 2012-03-02
  • 打赏
  • 举报
回复
std::strcmp
zyx1009 2012-03-02
  • 打赏
  • 举报
回复
呃 某个词语的次数 理解就好嘛
q2920 2012-03-02
  • 打赏
  • 举报
回复
我就郁闷了 你输入文字 查询单词?

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧