Essential C++学习(三)泛型编程风格——代码练习

练习 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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值