[Thinking in C++]C02:Ex04 的疑惑

matrixdwy 2008-03-09 05:07:48
Problem:Create a program that counts the occurrence of a particular word in a file (use the string class’ operator‘==’ to find the word).

问题:创建一个程序,能够计算某一个特定单词在文件中出现的次数(使用==符号来寻找这个单词)

我的解答:

 //:02:Quiz04
#include<vector>
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
void main(){
int num=0;
string word,a="ba";
vector<string>words;
ifstream in("1.txt");
while(in>>word)
words.push_back(word);
for(int i=0;i<words.size();i++)
{
if(a==words[i])
num+=1;
}
cout<<num<<endl;
}


问题是,在CHAPTER02中没有介绍过IF的语法,那有没有办法可以不用IF就可以实现预期目标呢?

...全文
55 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
matrixdwy 2008-03-11
  • 打赏
  • 举报
回复
网上找到标准答案,原来根本用不到vector,看来自己写的复杂了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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