第十章 习题10-21-习题10-30

习题10-21

#include<algorithm>
#include<iostream>
using namespace std;
void main()
{
	int val = 5;
	auto f = [&val]()mutable->bool {
		if (0 == val) 
			return false;
		else 
			return true;

	};
	while (f()) {
		--val;
		cout << val << endl;
	}
}

习题10-22

#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool isBiger2(const string &str,size_t sz)
{
	return str.size() <= sz;
}
void main()
{
	vector<string> vec = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	int a = 6;
	auto isBiger = bind(isBiger2, _1, a);
	cout << count_if(vec.begin(), vec.end(), isBiger) << endl;
}

习题10-23

bind接受无数个,1个可调用对象,和n个参数;

习题10-24

#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const string &str, size_t sz)
{
	return str.size() < sz;
}
void main()
{
	vector<int> vec = { 1,2,3,4,5,6 };
	string str = "big";
	auto it = find_if(vec.begin(), vec.end(), bind(check_size, str,_1));
	cout<<*it<< endl;
}

习题10-25

partition(words.begin(), words.end(), bind(check_size, _1, 6));

习题10-26

书上有,不做赘述

习题10-27

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include<iterator>
using namespace std;
int main()
{
	vector<int> vec{ 1,2,3,2,1};
	list<int> lst;

	sort(vec.begin(), vec.end());
	unique_copy(vec.begin(), vec.end(), back_inserter(lst));
	for (auto i : lst) cout << i << " ";
	cout << endl;
}

习题10-28

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include<iterator>
using namespace std;
int main()
{
	vector<int> vec{ 1,2,3,4 };
	list<int> lst1,lst2,lst3;
	auto iter = lst3.begin();
	copy(vec.begin(), vec.end(), back_inserter(lst1));
	copy(vec.begin(), vec.end(), front_inserter(lst2));
	copy(vec.begin(), vec.end(), inserter(lst3,iter));
	for (auto i : lst1) cout << i << " ";
	cout << endl;
	for (auto i : lst2) cout << i << " ";
	cout << endl;
	for (auto i : lst3) cout << i << " ";
	cout << endl;
}

习题10-29

#include <iostream>
#include<fstream>
#include<algorithm>
#include <vector>
#include<string>
#include<iterator>
using namespace std;
int main()
{
	vector<string> vec;
	ifstream infile("data.txt");
	istream_iterator<string> str_in(infile);
	istream_iterator<string> str_eof;
	while (str_in != str_eof)
		vec.push_back(*str_in++);
	ostream_iterator<string> out(cout, " ");
	copy(vec.begin(), vec.end(), out);
	cout << endl;
}

习题10-30

#include <iostream>
#include<algorithm>
#include <vector>
#include<iterator>
using namespace std;
int main()
{
	istream_iterator<int> int_in(cin), int_eof;
	ostream_iterator<int> out(cout, " ");
	vector<int> vec(int_in, int_eof);
	sort(vec.begin(), vec.end());
	copy(vec.begin(), vec.end(), out);
	cout << endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值