C++Primer 第10章 bind与迭代器

C++Primer 第10章 bind与迭代器

//10.20,10.22
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
using namespace std::placeholders;

bool sumcount(const string &str, const string::size_type &size)
{
	return str.size() == size;
}

int main()
{
	vector<string> str{ "wo","shi","ni","ba","ba!" };
	const string::size_type &sz = 2;
	cout << count_if(str.begin(), str.end(), bind(sumcount, _1, sz)) << endl;
	cout << count_if(str.begin(), str.end(), [&](const string &str) {return str.size() == sz; }) << endl;
}

//10.24
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
using namespace ::std;
using namespace ::std::placeholders;
bool strsize(string::size_type size,const string &str)
{
	return size == str.size();
}
int main()
{
	string str{ "abcd" };
	vector<int> ival{ 1,2,3,0xffff,4,5,6,7,8,9,010 };
	auto p = find_if(ival.begin(), ival.end(), bind(strsize, _1, str));
	cout << *p << endl;
	auto q = find_if(ival.begin(), ival.end(), [str](string::size_type size) {return size == str.size(); });
	cout << *q << endl;
}


10.4.2思考
#include<iostream>
#include<vector>
#include<iterator>
#include<numeric>
#include<xstddef>
using namespace std;
int main()
{
	//位于iterator头文件中
	istream_iterator<int> int_it(cin);
	istream_iterator<int> int_eof;
	//位于numeric头文件中
	//第一次看懂仿函数哈哈
	//cout << accumulate(int_it, int_eof,
	//	0/*初值*/, 
	//	/*仿函数*/minus</*默认传入迭代器右值类型*/>()/*这里默认使用plus<>()*/) << endl;

	//初始化输入
	vector<int> vint2(int_it, int_eof);
	ostream_iterator<int> int_ot(cout, " ");
	for (auto it : vint2)
		*******++***++**++***int_ot++++++ = it;
	//*int_ot++=it;		此迭代器++,*等操作符不做任何事情,已看源码,这些操作符的重载返回只是它本身
	//思考的原因:io流没有<<或>>这个并没有移动迭代器,迭代器只是模拟其他的迭代器保持统一的格式
	//but,这个东西没有--因为这个符合io流的逻辑
	//	cout << it << endl;
	copy(vint2.begin(), vint2.end(), int_ot);
	元素赋值
	//vector<int> vint;
	//while (int_it != int_eof)
	//	vint.push_back(*int_it++);
	//for (auto it : vint)
	//	cout << it << endl;
}

//10.29
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<fstream>
using namespace std;
int main()
{
	ifstream filein("data");
	if (!filein)
		cerr << "无法读取文件" << endl;
	string str1;
	//必须包含头文件string,itreator
	istream_iterator<string> str_it(filein);	
	istream_iterator<string> eof;
	vector<string> vstr1(str_it, eof);
	ostream_iterator<string> str_ot(cout, "\n");
	for (auto t : vstr1)
		str_ot = t;
	filein.close();
}

//10.30,10.31
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	istream_iterator<int> int_it(cin);
	istream_iterator<int> int_eof;
	ostream_iterator<int> int_ot(cout, ",");
	vector<int> vint1(int_it, int_eof);
	sort(vint1.begin(), vint1.end(), greater<>());
	unique_copy(vint1.begin(), vint1.end(), int_ot);
}

//10.33
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<fstream>
#include<algorithm>
#include<functional>
using namespace std;
using namespace std::placeholders;
using ostit = ostream_iterator<int>;
using istit = istream_iterator<int>;
void fenjiou(const int &ot, ostit &int_ot1, ostit &int_ot2)
{
	if (ot & 1)
		int_ot1 = ot;
	else
		int_ot2 = ot;
}
int main()
{
	ifstream filein1("data1");
	if (!filein1)
	{
		cerr << "无法读取文件" << endl;
		exit(0);
	}
	istit int_it(filein1);
	istit int_eof;
	vector<int> vint(int_it, int_eof);
	ofstream fileout1("jishu");
	if (!fileout1)
	{
		cerr << "无法读取文件" << endl;
		exit(0);
	}
	ofstream fileout2("oushu");
	if (!fileout2)
	{
		cerr << "无法读取文件" << endl;
		exit(0);
	}
	ostit fileji_ot(fileout1," ");
	ostit fileou_ot(fileout2, " ");
	for_each(vint.begin(), vint.end(), bind(fenjiou, _1, fileji_ot, fileou_ot));
	filein1.close();
	fileout1.close();
	fileout2.close();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值