老师布置的作业出问题啦!
要求:查找文件中特定单词的数量。要求用到vector和string 类的"==".
我写的代码:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int j=0;
string str;
vector<string> v;
ifstream in("TestFile.txt");
while (in>>str)//每次读入一个单词;
v.push_back(str);
for(int i=0;i<v.size();i++)
{
if(v[i]=="copy")
j++;
}
cout<<j<<endl;
}
TestFile.txt 文件的内容是:
copy copy copy copy, enter this is a test file.
出现的问题:结果为3,我想是因为最后一个copy后面有个逗号.高手来改下,让它过滤掉这个逗号啊!