Use of stream iterator

Source: Internet
Author: User

10.29 write a program and use the stream iterator to read a text file and store it in a string in a vector.

#include<iostream>#include<vector>#include<fstream>#include<string>#include<iterator>using namespace std;int main(){    ifstream in("1.txt");    istream_iterator<string> in_iter(in),eof;    ostream_iterator<string> out_iter(cout," ");    vector<string> vec;    while(in_iter!=eof)        vec.push_back(*in_iter++);    for(auto v:vec)        out_iter=v;    cout<<endl;    return 0;}

 

10.30 use stream iterator, sort, and copy to read an integer sequence from the standard input, sort it, and write the result to the standard output.

#include<iostream>#include<vector>#include<fstream>#include<iterator>#include<algorithm>using namespace std;int main(){    istream_iterator<int> in_iter(cin),eof;    ostream_iterator<int> out_iter(cout," ");    vector<int> vec;    while(in_iter!=eof)        vec.push_back(*in_iter++);    sort(vec.begin(),vec.end());    copy(vec.begin(),vec.end(),out_iter);    cout<<endl;    return 0;}

 

10.33 write a program and accept three parameters: one input file and two output file names. The input file must be an integer. Use istream_iterator to read text input files. Use ostream_iterator to write odd numbers to the first output file. Each value is followed by a space. Writes an even number to the second output file. Each value occupies an exclusive row.

#include<iostream>#include<vector>#include<fstream>#include<iterator>using namespace std;int main(){    ifstream in("1.txt");    ofstream out1("2.txt");    ofstream out2("3.txt");    istream_iterator<int> in_iter(in),eof;    ostream_iterator<int> out_iter1(out1," ");    ostream_iterator<int> out_iter2(out2,"\n");    vector<int> vec;    while(in_iter!=eof)        vec.push_back(*in_iter++);    for(auto v:vec)    {        if(v%2)          out_iter1=v;        else          out_iter2=v;    }    cout<<endl;    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.