Essential c++ 第三章课后练习

P75 find()函数处理array、vector、list的区别。

#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<iterator>
using namespace std;
int main()
{
	const int asize = 8;
	int ia[asize] = { 1, 1, 2, 3, 4, 5, 13, 21 };

	vector<int> ivec = { ia, ia + asize };
	list<int> ilist = { ia, ia + asize };

	int *pia = find(ia, ia + asize, 1024);       //返回一个iterator
	if (pia != ia + asize)
		cout << "find it1 " << endl;
	else
		cout << "not find it1" << endl;
	
	vector<int>::iterator iv;                //定义一个泛型指针iv
	iv = find(ivec.begin(), ivec.end(), 13);            //返回一个iterator指向该元素
	if (iv != ivec.end())
		cout << "find it2 " << endl;
	else
		cout << "not find it2" << endl;

	list<int>::iterator il;
	il = find(ilist.begin(), ilist.end(), 1024);
	if (il != ilist.end())
		cout << "find it3 " << endl;
	else
		cout << "not find it3" << endl;

	system("pause");
	return 0;
}

3.6小节 造轮子之filter()

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

template<typename InputIterator,typename OutputIterator,typename ElemType, typename Comp>
OutputIterator filter(InputIterator first, OutputIterator last, OutputIterator at, const ElemType &val, Comp pred)
{
	while ((first = find_if(first, last, bind2nd(pred, val))) != last)    
		//这里的pred是function object;bind2nd 是将val绑定于pred(二元运算)的第二个参数上。
	{
		cout << "found value: " << *first << endl;
		*at++ = *first++;   //先将at,first的iterator增加,然后令两者的值相等
	}
	return at;
}

int main()
{
	const int elem_size = 8;

	int ia[elem_size] = { 12, 8, 43, 0, 6, 21, 3, 7 };
	vector<int> ivec(ia, ia + elem_size);

	int ia2[elem_size];
	vector<int> ivec2(ia2, ia2 + elem_size);

	cout << "filtering integer array for values less than 8" << endl;
	filter(ia, ia + elem_size, ia2, elem_size, less<int>());

	cout << "filtering integer array for values more than 8" << endl;
	filter(ia, ia + elem_size, ia2, elem_size, greater<int>());

	system("pause");
	return 0;
}

3.6节 P89 sub_vec改写为template function

#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<functional>

using namespace std;

template<typename InputIterator, typename OutputIterator, typename ElemType, typename Comp>
OutputIterator sub_vec(InputIterator first, OutputIterator last, ElemType &val, Comp pred)
{

	OutputIterator iter = find_if(first, last, bind2nd(pred, val));      //返回一个iterator	
	return iter;
}


int main()
{
	int ar[8] = { 1, 5, 3, 7, 32, 33, 12, 9 };

	vector<int> ivec(ar, ar + 8);

	const int val = 8;

	sort(ivec.begin(), ivec.end(),greater<int>());   // 从大到小排列

	vector<int>::iterator iter = sub_vec(ivec.begin(),ivec.end(),val,less<int>());
	
	ivec.erase(iter, ivec.end());

	for (int i = 0; i < ivec.size(); ++i)
		cout << ivec[i] << endl;

	system("pause");
}

练习3.1

#include<iostream>
#include<string>
#include<fstream>
#include<map>
#include<set>
#include<iterator>
using namespace std;

int main()
{
	//打开已经创建好的TXT文件
	ifstream InputFile("practice3_1.txt");
	
	//生成一个set
	string ia[6] = { "a", "an", "or", "the", "and", "but" };
	set<string> WordEclusion(ia, ia + 6);
    
	map<string, int> dic;
	string str;
	//依次输入InputFile里的单词(没有了while循环结束),并且用set.count统计该单词是否属于排除set里。
	//往map里输入元素只需 map[ str] ++ 即可
	while (InputFile >> str)
	{
		
		if (WordEclusion.count(str))
			continue;
		dic[str]++;
	}
	//用for循环打印map里面的元素及其出现次数
	//用iterator, 剪头符号,key对应first,value对应second
	map<string, int>::iterator iter = dic.begin();    
	for (; iter != dic.end();++iter) 
	{
		cout << "the key: " << iter->first << " the value: " << iter->second << endl;
	}

	system("pause");

}

练习3.2

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<set>
#include<iterator>
#include<functional>
#include<algorithm>
using namespace std;

class LessThan         // 自定义一个函数对象(function object),用于控制sort()按字符串长度排序
{
public:
	bool operator()(const string &s1, const string &s2)
	{
		return s1.size() < s2.size();
	}
};
int main()
{
	//打开已经创建好的TXT文件
	ifstream InputFile("practice3_1.txt");
	
	//生成一个set
	string ia[6] = { "a", "an", "or", "the", "and", "but" };
	set<string> WordEclusion(ia, ia + 6);
    
	string str;
	vector<string> vec;

	while (InputFile >> str)
	{
		if (WordEclusion.count(str))
			continue;
		vec.push_back(str);
	}

	sort(vec.begin(), vec.end(),LessThan());    //调用自定义的函数对象LessThan()

	for (int i = 0; i < vec.size(); ++i)
		cout << vec[i] << endl;

	

	system("pause");

}

练习3.3

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

int main()
{
	//打开两个分别定义了家庭名和孩子名字的文件
	ifstream FamilyName("family_name.txt");
	ifstream ChildrenName("children_name.txt");

	map<string, string> dic;
	string str1,str2;
	vector<string> vec1,vec2;
	//分别将两个信息存入两个vector
	while (FamilyName >> str1)
		vec1.push_back(str1);
	while (ChildrenName>>str2)
		vec2.push_back(str2);
	//生成map
	for (int i = 0; i < vec1.size(); ++i)
		dic[vec1[i]] = vec2[i];
	//允许用户按姓氏查询
	string search;
	cout << "Which family name does you want to search:  ";
	cin >> search;
	cout << "The children name in this family is: " << dic[search] << endl;

	system("pause");
}

练习3.4

利用istream_iterator从标准输入设备读取一连串整数。利用ostream_iterator将其中的奇数写入某个文件,每个数值皆以空格分隔。偶数同理。

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std;

int main()
{
	istream_iterator<int> is(cin);
	istream_iterator<int> eof;

	ofstream odd_file("odd_file.txt");
	ofstream even_file("even_file.txt");

	vector<int> num;
	copy(is, eof, back_inserter(num));

	vector<int>num_odd, num_even;
	vector<int>::iterator iter=num.begin();
	for (; iter != num.end(); ++iter)
	{
		if ((*iter) % 2 == 0)
			num_even.push_back(*iter);
		else
			num_odd.push_back(*iter);
	}

	ostream_iterator<int> os1(odd_file, " ");
	copy(num_odd.begin(), num_odd.end(), os1);

	ostream_iterator<int> os2(even_file, " ");
	copy(num_even.begin(), num_even.end(), os2);

	system("pause");
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值