C++ Primer第十章练习题

10.23 bind函数的使用
bool isLesser(const string &s, string::size_type sz)
{
	return s.size() < sz;
}

int main()
{
	vector<string> authors{ "Mooophy", "pezy", "Queequeg90", "shbling", "evan617" };
	cout << count_if(authors.begin(), authors.end(), bind(isLesser, _1, 5));
	return 0;
}

10.30 istream_iterator的使用
int main()
{	
	vector<int> v;
	istream_iterator<int> int_it(cin), int_eof;
	copy(int_it, int_eof, back_inserter(v));
	sort(v.begin(), v.end());
	copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
	return 0;
}
10.33 编写程序,接受三个参数:一个输入文件和两个输出文件的文件名。输入文件保存的应该是整数。使用 istream_iterator 读取输入文件。使用 ostream_iterator 将奇数写入第一个输入文件,每个值后面都跟一个空格。将偶数写入第二个输出文件,每个值都独占一行。
int main()
{	
	ifstream ifs("D:\\SourceCode\\VSCode\\C++\\test1.txt");

	ofstream ofs_odd("D:\\SourceCode\\VSCode\\C++\\test2.txt");
	ofstream ofs_even("D:\\SourceCode\\VSCode\\C++\\test3.txt");

	istream_iterator<int> in(ifs), in_eof;
	ostream_iterator<int> out_odd(ofs_odd, " "), out_even(ofs_even, "\n");

	for_each(in, in_eof, [&out_odd, &out_even](const int i) {
		*(i & 1 ? out_odd : out_even)++ = i;
	});
	return 0;
}
发布了96 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/L_H_L/article/details/85285282