//把要排除的单词 存如vector中, 然后输出单词出现的次数.
#include<iostream>
#include<set>
#include<map>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
void restricted_wc(ifstream &removeFile,map<string,int> &wordCount)
{
vector<string>excluded;
string removeWord;
while(removeFile>>removeWord)
{
excluded.push_back(removeWord);
}
cout<<"enter text:"<<endl;
string word;
while(cin>>word)
{
bool find=false;
vector<string>::iterator iter=excluded.begin();
while(iter!=excluded.end())
{
if(*iter==word)
{
find=true;
break;
}
++iter;
}
if(!find)
++wordCount[word];
}
}
int main()
{
map<string,int> wordCount;
string fileName;
cout<<"enter filename:"<<endl;
cin>>fileName;
ifstream exFile(fileName.c_str());
if(!exFile) //代码正确,程序在打开文件时,总是提示"文件不能打开".是不是文件没放到正确位置啊~~~.请教~~~!
{
cout<<"error: can not open file:"<<fileName<<endl;
return -1;
}
restricted_wc(exFile,wordCount);
cout<<"word\t\t"<<"time"<<endl;
map<string,int>::iterator iter=wordCount.begin();
while(iter!=wordCount.end())
{
cout<<iter->first<<" "<<iter->second<<endl;
++iter;
}
return 0;
}
[此贴子已经被作者于2007-10-21 11:18:31编辑过]