c++ primer 练习10.4.2
本节练习主要是关于stream iterator的。这个组件感觉很有用!在数据输入输出,初始化,与STL algorithm的交互方面都有很大的作用。代码如下:
#include <iostream>
#include <fstream>
#include <sstream>
#include <numeric>
#include <vector>
#include <iterator>
#include <algorithm>
#include "Sales_item"
using namespace std;
void func_1029(vector<string> &vst);
void func_1030(vector<int> &vst);
void func_1031(vector<int> &vst);
void func_1032(vector<Sales_item> &total);
void func_1033(const string &infile, const string &outfile1, const string &outfile2);
bool CompareIsbn(const Sales_item &s1, const Sales_item &s2) { return s1.isbn() < s2.isbn(); }
int main(){
vector<string> vst;
vector<int> vin;
vector<Sales_item> vtrans;
// func_1029(vst);
// func_1030(vin);
// func_1031(vin);
// func_1032(vtrans);
func_1033("infile_1033", "outfile1_1033", "outfile2_1033");
return 0;
}
/*
* func_1029: we use istream_iterator to read and write data
* instead of "for" expression, it could be more succinct;
* * * * * * * * * * * * * * * * * * * * * */
void func_1029(vector<string> &vst){
ifstream infile("infileTest10_4_2");
istream_iterator<string> in_itr(infile), in_eof;
copy(in_itr, in_eof, back_inserter(vst));
ostream_iterator<string> out(cout, " ");
copy(vst.cbegin(), vst.cend(), out);
}
/*
* func_1030: we use stream iterator and STL algorithm
* to sort, input and output;
* * * * * * * * * * * * * * * * * * * * * */
void func_1030(vector<int> &vst){
istream_iterator<int> in(cin), in_eof;
ostream_iterator<int> out(cout, " ");
copy(in, in_eof, back_inserter(vst));
sort(vst.begin(), vst.end());
copy(vst.cbegin(), vst.cend(), out);
}
/*
* func_1031: we use stream iterator and STL algorithm
* to stable sort, input and output;
* * * * * * * * * * * * * * * * * * * * * */
void func_1031(vector<int> &vst){
istream_iterator<int> in(cin), in_eof;
ostream_iterator<int> out(cout, " ");
copy(in, in_eof, back_inserter(vst));
stable_sort(vst.begin(), vst.end());
unique_copy(vst.cbegin(), vst.cend(), out);
}
/*
* func_1032: we use stream iterator and STL algorothm
* to rewrite Sales_item counting program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void func_1032(vector<Sales_item> &total){
istream_iterator<Sales_item> in_itr(cin), in_eof;
ostream_iterator<Sales_item> out(cout, "\n");
copy(in_itr, in_eof, back_inserter(total));
sort(total.begin(), total.end(), CompareIsbn);
auto s_itr = total.cbegin();
auto s_itr0 = s_itr;
while( (s_itr=find_if(s_itr0, total.cend(),
[s_itr] (const Sales_item &trans) { return s_itr->isbn() != trans.isbn(); }))
!= total.cend()){
auto sum = accumulate(s_itr0, s_itr, Sales_item(s_itr0->isbn()));
*out++ = sum;
s_itr0 = s_itr;
}
auto sum = accumulate(s_itr0, s_itr, Sales_item(s_itr0->isbn()));
*out++ = sum;
s_itr0 = s_itr;
}
/*
* func_1033: we use stream iterator to read and write file
* * * * * * * * * * * * * * * * * * * * * */
void func_1033(const string &infile, const string &outfile1, const string &outfile2){
ifstream instrm_f(infile);
istream_iterator<int> in_itr(instrm_f), in_eof;
vector<int> vin(in_itr, in_eof);
ofstream outstrm_f1(outfile1);
ofstream outstrm_f2(outfile2);
ostream_iterator<int> out1(outstrm_f1, " ");
ostream_iterator<int> out2(outstrm_f2, "\n");
copy_if(vin.cbegin(), vin.cend(), out1, [] (const int val) { return val % 2; });
copy_if(vin.cbegin(), vin.cend(), out2, [] (const int val) { return !(val % 2); });//is it working???
}
109,1 Bot
92,2-9 73%
1,1 Top
来源:CSDN
作者:qq_38992862
链接:https://blog.csdn.net/qq_38992862/article/details/104226980