练习 3.1
void ask_usr(const map<string, int> &words, ostream &os)不对,在调用count = words[usr_ask];时报错,显示[]没有匹配运算符。
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<fstream>
#include<iterator>
#include<map>
#include<set>
using namespace std;
void display(const map<string, int> &words, ostream &os)
{
map<string,int>::const_iterator iter = words.begin();
map<string,int>::const_iterator end_it = words.end();
//如果words是空的,iter=words.end(),for循环不执行
for (; iter != end_it; ++iter)
os << "key: " << iter->first << "; value: " << iter->second << endl;
os << endl;
}
bool is_file_ok(ifstream &in_file, ofstream &out_file)
{
if (!in_file || !out_file)
{
cerr << "FBI warning\n";
return false;
}
return true;
}
void ask_usr(map<string, int> &words, ostream &os)
{
bool will = true;
string usr_ask;
char usr_req;
while (will == true)
{
cout << "请输入想要查询的单字" << endl;
cin >> usr_ask;
int count = 0;
if (words.count(usr_ask))//存在
{
count = words[usr_ask];
cout << usr_ask << "出现在文件中" << count << "次" << endl;
}
else
cout << "文件中没有" << usr_ask << "这个单字" << endl;
cout << "还需要查询吗? Y/N" << endl;
cin >> usr_req;
if (usr_req == 'N' || usr_req == 'n')
will = false;
}
}
int main()
{
ifstream infile("input_file.txt");
ofstream outfile("output_file.txt");
if (is_file_ok(infile, outfile))
{
istream_iterator<string> is(infile);
istream_iterator<string> eof;
vector<string> text;
//将文本文件的单字存入text
copy(is, eof, back_inserter(text));
//定义set集
string ia[] = { "a","an", "or", "and", "the", "but" };
vector<string> svec(ia, ia + 6);
set<string> word_exclusion(svec.begin(), svec.end());
//存入map
map<string, int> words;
for (int ix = 0; ix < text.size(); ++ix)
{
if (!(word_exclusion.count(text[ix]))) //判断是不是在set中
words[text[ix]]++; //填入map
}
//用户询问
ask_usr(words, cout);
//显示单字清单
display(words, outfile);
}
system("pause");
return 0;
}
练习3.2
定义funcitional object那不太懂,没用less<string>(),自己定义了一个bool less_than()
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<fstream>
#include<iterator>
#include<map>
#include<set>
#include<functional>
using namespace std;
void display(const map<string, int> &words, ostream &os)
{
map<string,int>::const_iterator iter = words.begin();
map<string,int>::const_iterator end_it = words.end();
//如果words是空的,iter=words.end(),for循环不执行
for (; iter != end_it; ++iter)
os << "key: " << iter->first << "; value: " << iter->second << endl;
os << endl;
}
bool is_file_ok(ifstream &in_file, ofstream &out_file)
{
if (!in_file || !out_file)
{
cerr << "FBI warning\n";
return false;
}
return true;
}
void ask_usr(map<string, int> &words, ostream &os)
{
bool will = true;
string usr_ask;
char usr_req;
while (will == true)
{
cout << "你需要查询吗? Y/N" << endl;
cin >> usr_req;
if (usr_req == 'N' || usr_req == 'n')
{
will = false;
continue;
}
cout << "请输入想要查询的单字" << endl;
cin >> usr_ask;
int count = 0;
if (words.count(usr_ask))//存在
{
count = words[usr_ask];
cout << usr_ask << "出现在文件中" << count << "次" << endl;
}
else
cout << "文件中没有" << usr_ask << "这个单字" << endl;
}
}
bool less_than(string s1, string s2)
{
return s1.length() < s2.length() ? true : false;
}
int main()
{
ifstream infile("input_file.txt");
ofstream outfile("output_file.txt");
if (is_file_ok(infile, outfile))
{
istream_iterator<string> is(infile);
istream_iterator<string> eof;
vector<string> vec;
//将文本文件的单字存入text
copy(is, eof, back_inserter(vec));
//定义set集
string ia[] = { "a","an", "or", "and", "the", "but" };
vector<string> svec(ia, ia + 6);
set<string> word_exclusion(svec.begin(), svec.end());
//存入map
map<string, int> words;
for (int ix = 0; ix < vec.size(); ++ix)
{
if (!(word_exclusion.count(vec[ix]))) //判断是不是在set中
words[vec[ix]]++; //填入map
}
//用户询问
ask_usr(words, cout);
//显示单字清单
display(words, outfile);
//对vec进行排序
sort(vec.begin(), vec.end(), less_than);
for (int ix = 0; ix < vec.size(); ++ix)
cout << vec[ix] << ' ';
cout << endl;
}
system("pause");
return 0;
}