ostream iterator

来源:互联网 发布:excel表格删除重复数据 编辑:程序博客网 时间:2024/06/05 17:26

1.ostream_iterator

template <class _Tp,
          class _CharT = char, class _Traits = char_traits<_CharT> >
class ostream_iterator {
public:
  typedef _CharT                         char_type;
  typedef _Traits                        traits_type;
  typedef basic_ostream<_CharT, _Traits> ostream_type;

  typedef output_iterator_tag            iterator_category;
  typedef void                           value_type;
  typedef void                           difference_type;
  typedef void                           pointer;
  typedef void                           reference;

  ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
  ostream_iterator(ostream_type& __s, const _CharT*__c)
    : _M_stream(&__s),_M_string(__c)  {}
  ostream_iterator<_Tp>& operator=(const _Tp& __value) {
    *_M_stream << __value;
    if (_M_string) *_M_stream <<_M_string;
    return *this;
  }
  ostream_iterator<_Tp>& operator*() { return *this; }
  ostream_iterator<_Tp>& operator++() { return *this; }
  ostream_iterator<_Tp>& operator++(int) { return *this; }
private:
  ostream_type* _M_stream;
  const _CharT* _M_string;

};

2.示例

http://www.cplusplus.com/reference/iterator/ostream_iterator/

// ostream_iterator example#include <iostream>     // std::cout#include <iterator>     // std::ostream_iterator#include <vector>       // std::vector#include <algorithm>    // std::copyint main () {  std::vector<int> myvector;  for (int i=1; i<10; ++i) myvector.push_back(i*10);  std::ostream_iterator<int> out_it (std::cout,", ");  std::copy ( myvector.begin(), myvector.end(), out_it );  return 0;}

运行结果: