[2018年4月30号]C++ primer 课后练习 第十章 泛型算法

10.26

back_inserter ,创建一个使用push_back的迭代器

front_inserter创建一个使用push_front的迭代器

inserter创建一个使用inserter的迭代器

10.27

int main(){
    vector<int > beCopy = {1,2,3,4,4,5,6,7,5,6,7,8};
    list<int > list_0(beCopy.size());
    sort(beCopy.begin(),beCopy.end());

    unique_copy(beCopy.begin(),beCopy.end(),list_0.begin());

    for_each(list_0.cbegin(), list_0.cend(),[](int i){ cout << i << endl;});

    for (;;);
    return 0;
}

10.28

inserter 和 back_inserter为123456789

front_inserter为987654321


10.29

int main(){
    ifstream file("test_2.txt");
    istream_iterator<string > in(file), eof;
    vector<string > vec;
    while (in != eof)
    {   
        vec.push_back(*in++);
    }
    for_each(vec.cbegin(),vec.cend(),[](const string& s){cout << s << endl;});
    for (;;);
    return 0;
}

10.30


10.31


10.32

10.33

ostream_iterator输出完之后,一定要刷新缓冲区!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

否则无法正确写入

void writeOddAndEven(ofstream& oddStream, ofstream& evenStream, ifstream& in){    
    istream_iterator<string > isin(in),eof;
    ostream_iterator<string > oOs(oddStream, " ");
    ostream_iterator<string > eOs(evenStream, " ");
    while (isin != eof)
    {
        if(stoi(*isin) %2){
            *oOs++ = *isin++;
        }else{
            *eOs++ = *isin++;
        }
    }
    oddStream << flush;
    evenStream << flush;
}
int main(){
    ifstream inFile("val.txt");
    ofstream odd("odd.txt");
    ofstream even("even.txt");
    writeOddAndEven(odd, even, inFile);
    for (;;);
    return 0;
}

10.34

int main(){
    vector<int > vec = {1,2,3,4,5,6,7,8};
    for(vector<int >::const_reverse_iterator it = vec.crbegin(); it != vec.crend(); it++){
        cout << *it << ends;
    }
    for (;;);
    return 0;
}

10.35

int main(){
    vector<int > vec = {1,2,3,4,5,6,7,8};
    for(vector<int >::iterator it = vec.end()-1; ; --it){
        cout << *it << ends;
        if(it == vec.begin()){
            break;
        }
    }
    for (;;);
    return 0;
}

10.36

int main(){
    list<int > listInt = {1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1,0,12};
    list<int >::const_reverse_iterator it = find(listInt.crbegin(),listInt.crend(),0);
    
    cout << *it << endl;
    for (;;);
    return 0;
}
10.37
int main(){
    vector<int > vecInt = {1,2,3,4,5,6,7,8,9,10};
    list<int > listInt(4);
    copy(vecInt.rbegin()+3, vecInt.rbegin()+7, listInt.begin());
    for_each(listInt.cbegin(),listInt.cend(),[]( const int & i){cout << i << ends;});
    
    for (;;);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值