草庐IT

ostream_iterator

全部标签

C++ ostream 和 ofstream 转换

我的代码有一个ostream对象,它由各种模块累积并最终显示到控制台。我还想将这个ostream对象写入文件,但是我是否必须改用ofstream对象重写所有代码,或者有没有办法将一个转换为另一个(可能通过stringstream?)例如,我现有的许多功能看起来像ostream&ClassObject::output(ostream&os)const{os我能否使用ofstream对象作为参数调用此函数,并让该ofstream对象代替累积信息? 最佳答案 是的,你可以。这就是OO概念中称为subtypepolymorphism的要点。

c++ - Clang 与 gcc std::crbegin with boost::iterator_range

使用libc++的Clang3.8.1编译以下程序:#include#include#include#include#includeintmain(){conststd::vectorv{1,2,3};constautorange=boost::make_iterator_range(v);std::copy(std::crbegin(range),std::crend(range),std::ostream_iterator{std::cout,""});std::cout但是带有libstdc++的gcc6.1.0没有。gcc错误的第一行是:error:nomatchingfunc

c++ - 我需要在 ostream 的头文件中包含什么

当我尝试编译我的程序时,编译器在我#included的.h文件中提示这一行。ostream&Print(ostream&stream);如何解决这个问题? 最佳答案 如果你#include,ostream将在std中定义命名空间:#include//...std::ostream&Print(std::ostream&stream); 关于c++-我需要在ostream的头文件中包含什么,我们在StackOverflow上找到一个类似的问题: https://

c++ - 使用自定义 std::ostream 包装 FILE*

我有一个与std::ostream一起工作的函数。我需要支持使用C文件句柄(FILE*)。我应该创建我自己的std::ostream的子类来委托(delegate)给FILE*吗? 最佳答案 正如BenVoigt所指出的,您想要子类化streambuf。南加州大学网站上的某些页面有documentation,header,和source对于包装FILE*的streambuf子类(stdiobuf)的GNU实现。它对作为(GroovX)一部分的库有一些依赖性,但这些应该很容易删除(我将从删除对GVX_TRACE的所有引用开始)。有趣的

c++ - 如何将 `std::vector<uchar>` 保存到 `std::ostream` 中?

我们已经创建并填充了一些std::vector与openCVimencode例如。现在我们想将它流式传输到一些http_lib中,它可以采用某种ostream。(ostringstream)例如,或者我们只是想在使用ofstream调试我们的程序时保存。所以我想知道如何把std::vector进入std::ostream? 最佳答案 使用write:voidsend_data(std::ostream&o,conststd::vector&v){o.write(reinterpret_cast(v.data()),v.size())

c++ - istream (ostream) 与 bool

这是一个读取尽可能多的单词的C++代码尽可能从给定的文本文件开始,直到遇到EOF。stringtext;fstreaminputStream;inputStream.open("filename.txt");while(inputStream>>text)cout我的问题是:将while循环的条件(即inputStream>>text)转换为bool值(即true或false)到底执行了什么过程?我自己对这个问题的回答是:据我了解,inputStream>>text应该返回另一个(文件)输入流。当EOF到达时,流似乎为NULL。NULL可以定义为0,相当于false。我的回答有道理吗?

c++ - stringstream临时ostream返回问题

我正在创建一个包含以下部分的记录器://#defineLOG(x)//forreleasemode#defineLOG(x)log(x)log(conststring&str);log(constostream&str);有了想法去做:LOG("Test");LOG(string("Testing")+"123");stringstreams;LOG(s这一切都按预期工作,但当我这样做时:LOG(stringstream()它不起作用:voidlog(constostream&os){std::streambuf*buf=os.rdbuf();if(buf&&typeid(*buf)=

c++ - 在类 C++ 中存储指向 istream 和 ostream 的指针

游戏.h#ifndefGAME_H#defineGAME_H#include#include#include"piece.h"usingnamespacestd;classGame{private:stringwhite;stringblack;stringtitle;istream*in;ostream*out;public:Game();Game(istream&,ostream&);voiddisplay(Colour,short);};#endif游戏.cpp#include#include#include#include"game.h"#include"board.h"#in

c++ - 基于范围的对 <Iterator,Iterator>

我对以下答案有疑问:https://stackoverflow.com/a/15828866/2160256如那里所述,我们不能像这样在BGL中使用基于范围的for:for(autoe:boost::edges(g))//dosomethingwithe然而,here它指出,我们可以重载使用基于范围的语义所需的begin()和end()函数。所以我尝试了:templateIbegin(std::pair&p){returnp.first;}templateIend(std::pair&p){returnp.second;}但是,编译器仍然报错:error:nomatchingfunct

c++ - 使用 istream_iterator 并从标准输入或文件中读取

我正在使用MicrosoftVisualC++编写程序,我希望我的程序使用istream_iterator从标准输入或文件中读取。谷歌搜索互联网并没有显示我认为它必须多么简单。因此,例如,我可以很容易地编写并从标准输入中读取:#include#include#includeusingnamespacestd;intmain(){istream_iteratormy_it(cin);for(;my_it!=istream_iterator();my_it++)printf("%s\n",(*my_it).c_str());}或者我可以写这个并从文件中读取:#include#include