istream_iterator 和 ostream_iterator

C++ primer code learning
binds standard input cin through istream_iterator and
binds standard output cout through ostream_iterator

#include <iterator>
using std::istream_iterator;
using std::ostream_iterator;

#include<vector>
using std::vector;

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

int main(int argc, char const *argv[])
{
    
    
    vector<int> vec;
    istream_iterator<int> in_iter(cin);
    istream_iterator<int> eof;		//结束符

    while (in_iter != eof)
    {
    
    
        vec.push_back(*in_iter);
        in_iter++;
    }
    ostream_iterator<int> out_iter(cout, " ");

    for(auto e : vec)
        out_iter = e;
    cout << endl;

    system("pause");   
    return 0;
}

这个地方有个小坑:
The input is of type int: when entering the while loop input through cin, the end of the data or the next line must be typed: Ctrl + Z and press Enter and line feed to end; the
input is of string type: when entering the while loop input through cin, the next line of data , type: Ctrl + Z and press Enter and line feed to end;

Input is string type

Input is of type int
Input is of type int

Input is string type

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/Ternence_zq/article/details/127782892