ostream

Why is istream/ostream slow

混江龙づ霸主 提交于 2019-11-27 18:31:05
At 50:40 of http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly Andrei Alexandrescu makes a joke about how not efficient/slow istream is. I had an issue in the past with ostream being slow and fwrite being significantly faster (reducing many seconds when running the main loop once) but I never understood why nor looked into it. What makes istream and ostream slow in C++? or at least slow compared to other things (like fread/fget, fwrite) which would equally satisfied the needs. Dietmar Kühl Actually, IOStreams don't have to be slow! It is a matter of implementing

Why can't I initialize a reference to `ofstream` / `ifstream`, with an instance of `fstream`?

為{幸葍}努か 提交于 2019-11-27 17:50:05
问题 INTRODUCTION void read_foo (std::ifstream& out); void write_foo (std::ofstream& out); I have these two functions where one is supposed to read from a file, and the other is supposed to write to one. Everything works having the below snippets; std::ifstream ifs ("filename.txt"); read_foo (ifs); std::ofstream ofs ("filename.txt"); write_foo (ofs); THE PROBLEM However, if I try to use a std::fstream , so I can call both functions with the same stream, it doesn't compile, and the compiler spits

Platform independent /dev/null in c++ [duplicate]

痞子三分冷 提交于 2019-11-27 14:33:14
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Implementing a no-op std::ostream Is there any stream equivalent of NULL in c++? I want to write a function that takes in a stream if the user wants to have the internal outputted to somewhere, but if not, the output goes into some fake place void data(std::stream & stream = fake_stream){ stream << "DATA" ; } i want to be able to chose to do data() or data(std::cout) 回答1: Edit : Taken from @Johannes Schaub -

How to make C++ cout not use scientific notation

家住魔仙堡 提交于 2019-11-27 12:59:31
double x = 1500; for(int k = 0; k<10 ; k++){ double t = 0; for(int i=0; i<12; i++){ t += x * 0.0675; x += x * 0.0675; } cout<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl; } this the output Bas ana: 3284.78 Son faiz: 1784.78 Son ana: 5069.55 Bas ana: 7193.17 Son faiz: 3908.4 Son ana: 11101.6 Bas ana: 15752 Son faiz: 8558.8 Son ana: 24310.8 Bas ana: 34494.5 Son faiz: 18742.5 Son ana: 53237 Bas ana: 75537.8 Son faiz: 41043.3 Son ana: 116581 Bas ana: 165417 Son faiz: 89878.7 Son ana: 255295 Bas ana: 362238 Son faiz: 196821 Son ana: 559059 Bas ana: 793246 Son faiz: 431009 Son ana: 1

How to inherit from std::ostream?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 12:03:19
I've been googling around and I just can't find a simple answer to this. And it should be simple, as the STL generally is. I want to define MyOStream which inherits publicly from std::ostream. Let's say I want to call foo() each time something is written into my stream. class MyOStream : public ostream { public: ... private: void foo() { ... } } I understand that the public interface of ostream is non-virtual, so how can it be done? I want clients to be able to use both operator<< and write() and put() on MyOStream and have use the extended ability of my class. It's not a simple question,

order of execution in operator<<

五迷三道 提交于 2019-11-27 02:15:10
I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below A1B2 While I can see that the output I get is BA12 I thought that the call std::cout<< b->fooA() << b->fooB() << std::endl was equivalent to call std::cout.operator<<( b->fooA() ).operator<< ( b->fooB() ) but I can see that this is not the case. Can you help me understanding better how this does it work and the relationship with the global operator<< ? Is this last ever called in this sequence? Regards AFAG #include <iostream> struct cbase{ int fooA(){ std::cout<<"A"; return 1;

Is there a null std::ostream implementation in C++ or libraries?

馋奶兔 提交于 2019-11-27 01:26:57
问题 I'm looking for a std::ostream implementation that acts like /dev/null . It would just ignore anything that is streamed to it. Does such a thing exist in the standard libraries or Boost? Or do I have to roll my own? 回答1: If you have boost, then there's a null ostream & istream implementation available in boost/iostreams/device/null.hpp . The gist of it: #include "boost/iostreams/stream.hpp" #include "boost/iostreams/device/null.hpp" ... boost::iostreams::stream< boost::iostreams::null_sink >

How do the stream manipulators work?

只谈情不闲聊 提交于 2019-11-27 01:21:34
It is well known that the user can define stream manipulators like this: ostream& tab(ostream & output) { return output<< '\t'; } And this can be used in main() like this: cout<<'a'<<tab<<'b'<<'c'<<endl; Please explain me how does this all work? If operator<< assumes as a second parameter a pointer to the function that takes and returns ostream & , then please explain my why it is necessary? What would be wrong if the function does not take and return ostream & but it was void instead of ostream & ? Also it is interesting why “dec”, “hex” manipulators take effect until I don’t change between

How do I create my own ostream/streambuf?

不问归期 提交于 2019-11-27 00:31:55
For educational purposes I want to create a ostream and stream buffer to do: fix endians when doing << myVar; store in a deque container instead of using std:cout or writing to a file log extra data, such as how many times I did <<, how many times I did .write, the amount of bytes I written and how many times I flush(). But I do not need all the info. I tried overloading but failed horribly. I tried overloading write by doing ostream& write( const char* s, streamsize n ) in my basic_stringstream2 class (I copied paste basic_stringstream into my cpp file and modified it) but the code kept using

C++ - How to declare a function template friend for a class template

吃可爱长大的小学妹 提交于 2019-11-26 21:40:54
问题 I have a class template that will output a list of objects stored in the array. I am getting the following error and I am confused where the error is caused since the error is in the .obj and .exe file. 1 unresolved externals (proj08.exe line 1) unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class MyVector)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@V?$MyVector@N@@@Z) referenced in function _main (porj08.obj line 1)