ostream

Check if ostream object is cout or ofstream, c++

丶灬走出姿态 提交于 2019-11-26 21:37:30
问题 Is there a way in C++ to check if an ostream object is cout or a ofstream object? Something like: ostream& output(ostream& out) { if (out == cout) return out; else { out << "something different because its not going to the console" << endl; return out; } } The reason I want to do this, is that I want to overload the << operator to do two different things depending on what type of stream it is used with. Is it possible to just overload the << operator twice each time with a different type of

How to use C++ std::ostream with printf-like formatting?

断了今生、忘了曾经 提交于 2019-11-26 20:33:11
问题 I am learning C++. cout is an instance of std::ostream class. How can I print formatted string with it? I still can use printf , but I want to learn more C++ style method which can take all C++ benefits. I think this should be possible with std::ostream but I can't find proper way. 回答1: The only thing you can do with std::ostream directly is the well known << -syntax: int i = 0; std::cout << "this is a number: " << i; And there are various IO manipulators that can be used to influence the

Why is istream/ostream slow

安稳与你 提交于 2019-11-26 19:18:40
问题 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

How to inherit from std::ostream?

泄露秘密 提交于 2019-11-26 15:53:27
问题 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()

how do I print an unsigned char as hex in c++ using ostream?

房东的猫 提交于 2019-11-26 14:17:00
I want to work with unsigned 8-bit variables in C++. Either unsigned char or uint8_t do the trick as far as the arithmetic is concerned (which is expected, since AFAIK uint8_t is just an alias for unsigned char , or so the debugger presents it. The problem is that if I print out the variables using ostream in C++ it treats it as char. If I have: unsigned char a = 0; unsigned char b = 0xff; cout << "a is " << hex << a <<"; b is " << hex << b << endl; then the output is: a is ^@; b is 377 instead of a is 0; b is ff I tried using uint8_t , but as I mentioned before, that's typedef'ed to unsigned

How do the stream manipulators work?

心不动则不痛 提交于 2019-11-26 09:37:28
问题 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

How do I create my own ostream/streambuf?

只愿长相守 提交于 2019-11-26 09:23:59
问题 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

how do I print an unsigned char as hex in c++ using ostream?

旧街凉风 提交于 2019-11-26 05:55:42
问题 I want to work with unsigned 8-bit variables in C++. Either unsigned char or uint8_t do the trick as far as the arithmetic is concerned (which is expected, since AFAIK uint8_t is just an alias for unsigned char , or so the debugger presents it. The problem is that if I print out the variables using ostream in C++ it treats it as char. If I have: unsigned char a = 0; unsigned char b = 0xff; cout << \"a is \" << hex << a <<\"; b is \" << hex << b << endl; then the output is: a is ^@; b is 377

Floating point format for std::ostream

北城以北 提交于 2019-11-26 04:46:55
问题 How do I do the following with std::cout? double my_double = 42.0; char str[12]; printf_s(\"%11.6lf\", my_double); // Prints \" 42.000000\" I am just about ready to give up and use sprintf_s. More generally, where can I find a reference on std::ostream formatting that lists everything in one place, rather than spreading it all out in a long tutorial? EDIT Dec 21, 2017 - See my answer below. It uses features that were not available when I asked this question in 2012. 回答1: std::cout << std:

How to properly overload the << operator for an ostream?

让人想犯罪 __ 提交于 2019-11-25 22:47:11
问题 I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++. Here is the relevant part of my matrix class: namespace Math { class Matrix { public: [...] friend std::ostream& operator<< (std::ostream& stream, const Matrix&