c++ primer 练习10.4.2

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值