`
shi5jin
  • 浏览: 37183 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

A Generic Ostream Iterator

阅读更多
by Christopher Diggins
November 11, 2005

    Summary
    Ostream iterators are a handy but under-utilized tool for using the STL to output containers and ranges. Here I provide an alternative, which has a more pleasing syntax.

An ostream iterator, allows you to use STL algorithms, such as std::copy to output a container or range to an output stream. A simple example is:

int main() {
  int array[] = { 1, 2, 4, 8, 16, 32, 64 };
  std::copy(array, array + 7, std::ostream_iterator<int>(std::cout, "\n"));
}

The declaration for an ostream_iterator is clunky because you to know the kind of elements it will be reading. You can make the code more readable as follows:

int main() {
  int array[] = { 1, 2, 4, 8, 16, 32, 64 };
  typedef std::ostream_iterator<int> oiter;
  std::copy(array, array + 7, oiter(std::cout, "\n"));
}

This is still more complex than strictly neccessary. The problem is that the ostream_iterator is a template. An alternative implementation would be to make the operator=() member function a template.

#include <iostream>

struct putter {
  putter(const putter& x) : o(x.o), delim(x.delim) { }
  putter(std::ostream& x = std::cout, const char* s = "") : o(x), delim(s) { }
  template<typename T>
  putter& operator=(const T& x) { o << x << delim; return *this; }
  putter& operator*() { return *this; }
  putter& operator++() { return *this; }
  putter& operator++(int) { return *this; }
  mutable std::ostream& o;
  const char* delim;
};

putter put(std::ostream& o = std::cout, const char* delim = "") {
  return putter(o, delim);
}

Now you can output the contents of any iterator pair with less work.

int main() {
  int array[] = { 1, 2, 4, 8, 16, 32, 64 };
  std::copy(array, array + 7, put(std::cout, "\n"));
}
分享到:
评论

相关推荐

    C语言头文件 OSTREAM.H

    C语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件...

    istream_iterator 读取输入文件

    C++ primer 第五版 中文版 练习 10.33 题目:编写程序,接受三个参数:一个输入文件和两个输出...使用ostream_iterator将奇数写入第一个输出文件,每个值后都跟一个空格。将偶数写入第二个输出文件,每个值都独占一行。

    ostream用法解释

    C++的ostream类的使用方法,很详细的。。

    流类库与输入输出(ostream,ofstream,ostringstream)

    三个重要的输出流: – ostream – ofstream – ostringstream

    C语言头文件 OSTREAM

    C语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言...

    ostream头文件(in Dev-cpp)

    c++文件输出类

    二维vector源码实现

     ostream_iterator&lt;int&gt; os(cout, " ");  Matrix[0][2] = 4;  //交换矩阵的两行  Matrix[0].swap(Matrix[1]);  //交换矩阵第二行的两个元素  swap(Matrix[1][0], Matrix[1][2]);  for (uint i = 0; i (); i++...

    audio_ostream-文本到语音的ostream

    一篇文章,说明如何使用ostream接口将“文本到语音”添加到应用程序

    Text-To-Speech-ostream

    文件包括语音合成以及语音识别的源代码,另外还有流的一些目录文件等

    文件输入输出流(总概)

    NULL 博文链接:https://pichcar.iteye.com/blog/316478

    audio_ostream_src.zip_C# TTS_tts

    TTS,TTS To Speech,Read the text using the microsoft TTS engine,and ACE libraly

    数据结构 二叉树原程序

    friend ostream& operator(ostream& out_stream, BinSearchTree&lt;T&gt;& tree) { for (Iterator itr = tree.begin(); itr != tree.end(); itr++) out_stream *itr ; return out_stream; } // overloading ...

    c-plus_learn:一些尝试学习

    使用istream_iterator和ostream-iterator来管理文本信息的转换。 3.我把它放在2之内。 4,使用map来存储txt中的信息,并排除列表中的某些单词。 5.类似于4,但输出文件以显示结果。 6.使用地图和向量查找家庭成员的...

    STL源码剖析.pdg

    8.3.3 stream iterators (istream_iterator, ostream_iterator) 442 8.4 function adapters 448 8.4.1 对传回值进行逻辑否定:not1, not2 450 8.4.2 对参数进行系结(绑定):bind1st, bind2nd 451 8.4.3 用于...

    C++自编String类代码

    friend ostream& operator(ostream&s,const String&a) { s&lt;&lt;a.str; return s; } friend istream& operator&gt;&gt;(istream&i,const String&a) { i&gt;&gt;a.str; return i; } ~String(); private: int len; ...

    Visual C++ 编程资源大全(英文源码 DLL)

    (4KB)&lt;END&gt;&lt;br&gt;99,tokeniterator.zip Token Iterator provides an easy to use, familiar, and customizable way in which to go through the tokens contained in a string (7KB)&lt;END&gt;&lt;br&gt;100,stdsort.zip ...

    STL copy()函数用法

    #include #include #include using namespace std;... ostream_iterator ofile&#40;cout, &#41;; //将数值复制到vector里,参数依次是开始,结束,vector数组的开始 //(看起来src+7越界了)src+7,表示结束,sr

    P_SG_CREATE_SCRIPT_BY_BATCH_CODE.sql

    gbase存储过程 gbase:致力于海量数据分析领域的应用,满足数知据密集型行业日益增大的关系型联机分析处理(ROLAP)、即席查询(Ad Hoc)、复杂报表等需求的高道性能数据处理应用。

    c++复习资料综合版本

    A、stream B、istream C、ostream D、iostream (C)4、下列对于内置函数的说法错误的是。 A、 主要作用是压缩运行时间的开销; B.一般内置函数的代码较少,5句左右; C.内置函数与调用函数可以放在不同文件中; D...

    LuaBind 源码 (Lua增强库)

    &gt; a = testclass('a string') &gt; a:print_string() a string 还可以注册自由函数作为成员函数.对这个自由函数的要求是,它必须接受该类的一个指针或常量指针或 引用或常量引用作为函数的第一个参数.该函数的剩下的...

Global site tag (gtag.js) - Google Analytics