c++ istream_iterator ostream_iterator

istream_iterator/ostream_iterator

void stream_iter_odd_even(const string &in_file, const string &odd_file, const string &even_file) {
    ifstream is(in_file);
    ofstream os_odd(odd_file);
    ofstream os_even(even_file);
    
    istream_iterator<int> is_iter(is), end;
    ostream_iterator<int> os_odd_iter(os_odd, " ");
    ostream_iterator<int> os_even_iter(os_even, "\n");
    
    //for_each(is_iter, end, [&](const int &num) -> void {
    //    if(num%2)
    //        *os_odd_iter++ = num;
    //    else
    //        *os_even_iter++ = num;
            
            
    int tmp;
    while(is_iter != end) {
        if(tmp = *is_iter++) % 2) 
            *os_odd_iter++ = num;
        else
            *os_even_iter++ = num;
    }
}
posted @ 2021-12-01 23:29  下夕阳  阅读(122)  评论(0编辑  收藏  举报