C++Primer 第10章 bind与迭代器
#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;
}
#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()
{
istream_iterator<int> int_it(cin);
istream_iterator<int> int_eof;
vector<int> vint2(int_it, int_eof);
ostream_iterator<int> int_ot(cout, " ");
for (auto it : vint2)
*******++***++**++***int_ot++++++ = it;
copy(vint2.begin(), vint2.end(), int_ot);
}
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ifstream filein("data");
if (!filein)
cerr << "无法读取文件" << endl;
string str1;
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();
}
#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);
}
#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();
}