/根据形参所指定的文件建立单词排除集
//被排除的单词放在vecter容器中
//并从标准输入设备读入文本,对不在排除集中的单词存在关联容器中
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
void wc(ifstream&remove_file,map<string,int> &word_count)
{
vector<string> excl;
stringremove_word;
while(remove_file >> remove_word)
{
excl.push_back(remove_word);
}
stringword;
while(cin>>word)
{
/*vector<string>::iteratorit = excl.begin();
for (;it !=excl.end();++it)
{
if ((*it)== word)
{
break;
}
}
if (it ==excl.end())
{
++word_count[word];
} */
if( find(excl.begin(),excl.end(),word) == excl.end() )
{
++word_count[word];
}
}
}
int main()
{
map<string,int>word_C;
stringfileName;
cout<<"Enter fileName:"<<endl;
cin>> fileName;
ifstreamexfile(fileName.c_str());
if(!exfile)
{
cout<<"error:没有找到该文件"<<endl;
return-1;
}
wc(exfile,word_C);
cout<< "输出结果如下:";
map<string,int>::iterator iter=word_C.begin();
while(iter != word_C.end())
{
cout<< iter->first<<" "<<iter->second<<endl;
++iter;
}
return0;
}
10.25
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
vector<string>Wread;
set<string>Hread;
stringname;
cout<<"建立未来个月要读的书的名字"<<endl;
while(cin >> name)
{
Wread.push_back(name);
}
cin.clear();
booltimeover = false;
stringanswer;
stringbookname;
srand((unsigned)time(NULL));
while(!Wread.empty()&& !timeover )
{
cout<<"would you like reading(Y/N)? "<<endl;
cin>>answer;
if(answer == "Y"||answer == "y")
{
//inti = (int)rand()% Wread.size();
//bookname = Wread[i];
cout<<"请输入你要读的书的名字"<<endl;
cin>>bookname;
//Wread.erase(Wread.begin()+i);
for (vector<string>::iteratorit = Wread.begin();it != Wread.end();++it)
{
if (*it == bookname)
{
Wread.erase(it);
break;
}
}
Hread.insert(bookname);
}
cout<< "did you read the book?"<<endl;
cin>>answer;
if(answer == "N"||answer == "n")
{
Hread.erase(bookname);
Wread.push_back(bookname);
}
cout<<"time if over?"<<endl;
cin>>answer;
if(answer == "Y"||answer == "y")
{
timeover = true;
}
}
if(timeover)
{
cout<< "没有读过的书:"<<endl;
for(vector<string>::iterator it = Wread.begin();it!= Wread.end();++it)
{
cout<< (*it)<<endl;
}
cout<<"读过的书:"<<endl;
for(set<string>::iterator it = Hread.begin();it != Hread.end();++it)
{
cout<<(*it);
}
}
return 0;
}