Problem:Create a program that counts the occurrence of a particular word in a file (use the string class’ operator‘==’ to find the word).
问题:创建一个程序,能够计算某一个特定单词在文件中出现的次数(使用==符号来寻找这个单词)
我的解答:
[code=C/C++] //: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;
}[/code]
问题是,在CHAPTER02中没有介绍过IF的语法,那有没有办法可以不用IF就可以实现预期目标呢?