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.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件 OSTREAM.HC语言头文件...
C++ primer 第五版 中文版 练习 10.33 题目:编写程序,接受三个参数:一个输入文件和两个输出...使用ostream_iterator将奇数写入第一个输出文件,每个值后都跟一个空格。将偶数写入第二个输出文件,每个值都独占一行。
C++的ostream类的使用方法,很详细的。。
三个重要的输出流: – ostream – ofstream – ostringstream
C语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言头文件 OSTREAMC语言...
c++文件输出类
ostream_iterator<int> os(cout, " "); Matrix[0][2] = 4; //交换矩阵的两行 Matrix[0].swap(Matrix[1]); //交换矩阵第二行的两个元素 swap(Matrix[1][0], Matrix[1][2]); for (uint i = 0; i (); i++...
一篇文章,说明如何使用ostream接口将“文本到语音”添加到应用程序
文件包括语音合成以及语音识别的源代码,另外还有流的一些目录文件等
NULL 博文链接:https://pichcar.iteye.com/blog/316478
TTS,TTS To Speech,Read the text using the microsoft TTS engine,and ACE libraly
friend ostream& operator(ostream& out_stream, BinSearchTree<T>& tree) { for (Iterator itr = tree.begin(); itr != tree.end(); itr++) out_stream *itr ; return out_stream; } // overloading ...
使用istream_iterator和ostream-iterator来管理文本信息的转换。 3.我把它放在2之内。 4,使用map来存储txt中的信息,并排除列表中的某些单词。 5.类似于4,但输出文件以显示结果。 6.使用地图和向量查找家庭成员的...
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 用于...
friend ostream& operator(ostream&s,const String&a) { s<<a.str; return s; } friend istream& operator>>(istream&i,const String&a) { i>>a.str; return i; } ~String(); private: int len; ...
(4KB)<END><br>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)<END><br>100,stdsort.zip ...
#include #include #include using namespace std;... ostream_iterator ofile(cout, ); //将数值复制到vector里,参数依次是开始,结束,vector数组的开始 //(看起来src+7越界了)src+7,表示结束,sr
gbase存储过程 gbase:致力于海量数据分析领域的应用,满足数知据密集型行业日益增大的关系型联机分析处理(ROLAP)、即席查询(Ad Hoc)、复杂报表等需求的高道性能数据处理应用。
A、stream B、istream C、ostream D、iostream (C)4、下列对于内置函数的说法错误的是。 A、 主要作用是压缩运行时间的开销; B.一般内置函数的代码较少,5句左右; C.内置函数与调用函数可以放在不同文件中; D...
> a = testclass('a string') > a:print_string() a string 还可以注册自由函数作为成员函数.对这个自由函数的要求是,它必须接受该类的一个指针或常量指针或 引用或常量引用作为函数的第一个参数.该函数的剩下的...