C++ Stream(串流)迭代器

C++ Stream(串流)迭代器

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    ostream_iterator<int> intWriter(cout,"
");

    *intWriter = 42;
    intWriter++;

    *intWriter = 77;
    intWriter++;
    *intWriter = -5;

    vector<int> vector1 = {1,2,3,4,5,6,7,8,9};
    copy(vector1.cbegin(),vector1.cend(),ostream_iterator<int>(cout));
    cout << endl;

    copy(vector1.cbegin(),vector1.cend(),ostream_iterator<int>(cout,"  <  "));
    cout << endl;

    system("pause");
    return 0;
}

42
77
-5
123456789
1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 <
请按任意键继续. . .

代码参考:C++标准库(第2版)

原文地址:https://www.cnblogs.com/herd/p/12121439.html