ostream

Programmatically Ignore Cout

我怕爱的太早我们不能终老 提交于 2019-11-30 16:55:17
问题 Does anybody know if there is a trick to toggle all the cout << functions to not print out visible output? I am trying to hack together some code written by me and some other people to put together a demo. I would rather not redirect the output to a file and would like a solution that had some measure of compatibility between Windows and Linux. In my scenario I have many many lines of code with with various #defines controlling when certain methods produce debug output. I want to call

why is std::cout convertible to void* if using g++?

我们两清 提交于 2019-11-30 14:47:34
问题 Why can one cast a std::ostream to a void pointer? I am not aware of any such conversion operator in std::ostream . Code below #include <iostream> int main() { void *p = std::cout; // why does this work? } I'm asking this question since I've seen a placement operator new invoked as Foo* pFoo = new (std::cerr) Foo; and have absolutely no idea why would one write such a thing. PS: I am compiling with g++ 4.9.2 with or without -std=c++11 . clang++ does not accept the code . PSS: Found out that

Does Overloading Operator<< works inside the class?

喜你入骨 提交于 2019-11-30 14:12:37
问题 I mean, I was trying to overload the operator<< inside the class like this class A { public: ostream &operator<<(ostream &os);// which doesnt work private: friend ostream &operator<<(ostream &os, const A& a); //Works int i; }; Definition ostream &operator<<(ostream &os, const A& a) { os<<a.i; return os; } why can't I overload the operator inside the class specific to class? or Am I missing something? or Am I stupid to even think in such a way? Please advise. 回答1: The member function: ostream

Center text in fixed-width field with stream manipulators in C++

只愿长相守 提交于 2019-11-30 13:14:30
问题 I am refactoring some legacy code which is using printf with longs strings (without any actual formatting) to print out plain text table headers which looks notionally like this: | Table | Column | Header | which are currently being produced like this: printf("| Table | Column | Header |"); I would like to produce the above with code to the effect of 1 : outputStream << "|" << std::setw(10) << std::center << "Table" << "|" << std::setw(10) << std::center << "Column" << "|" << std::setw(9) <<

How to write 'n' copies of a character to ostream like in python

本秂侑毒 提交于 2019-11-30 12:36:13
In python, the following instruction: print 'a'*5 would output aaaaa . How would one write something similar in C++ in conjunction with std::ostream s in order to avoid a for construct? The obvious way would be with fill_n : std::fill_n(std::ostream_iterator<char>(std::cout), 5, 'a'); Another possibility would be be to just construct a string: std:cout << std::string(5, 'a'); Use some tricky way: os << setw(n) << setfill(c) << ""; Where n is number of char c to write You can do something like that by overloading the * operator for std::string. Here is a small example #include<iostream>

why is std::cout convertible to void* if using g++?

邮差的信 提交于 2019-11-30 11:43:41
Why can one cast a std::ostream to a void pointer? I am not aware of any such conversion operator in std::ostream . Code below #include <iostream> int main() { void *p = std::cout; // why does this work? } I'm asking this question since I've seen a placement operator new invoked as Foo* pFoo = new (std::cerr) Foo; and have absolutely no idea why would one write such a thing. PS: I am compiling with g++ 4.9.2 with or without -std=c++11 . clang++ does not accept the code . PSS: Found out that due to the so called "safe bool problem" (see @nicebyte's answer), in pre C++11 a void* conversion

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

时光毁灭记忆、已成空白 提交于 2019-11-30 11:30:55
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 out a lot of error messages. Why can't I bind a fstream to an ifstream& , or ofstream& ? foo.cpp

Does Overloading Operator<< works inside the class?

◇◆丶佛笑我妖孽 提交于 2019-11-30 09:15:33
I mean, I was trying to overload the operator<< inside the class like this class A { public: ostream &operator<<(ostream &os);// which doesnt work private: friend ostream &operator<<(ostream &os, const A& a); //Works int i; }; Definition ostream &operator<<(ostream &os, const A& a) { os<<a.i; return os; } why can't I overload the operator inside the class specific to class? or Am I missing something? or Am I stupid to even think in such a way? Please advise. The member function: ostream &operator<<(ostream &os); does work, but not for the situation you want. It will be called when you do

Center text in fixed-width field with stream manipulators in C++

若如初见. 提交于 2019-11-30 06:52:34
I am refactoring some legacy code which is using printf with longs strings (without any actual formatting) to print out plain text table headers which looks notionally like this: | Table | Column | Header | which are currently being produced like this: printf("| Table | Column | Header |"); I would like to produce the above with code to the effect of 1 : outputStream << "|" << std::setw(10) << std::center << "Table" << "|" << std::setw(10) << std::center << "Column" << "|" << std::setw(9) << std::center << "Header" << "|" << std::endl; which does not compile because <iomanip> has the stream

How to store formatting settings with an IOStream?

最后都变了- 提交于 2019-11-30 03:23:20
问题 When creating formatted output for a user defined type it is often desirable to define custom formatting flags. For example, it would be nice if a custom string class could optionally add quotes around the string: String str("example"); std::cout << str << ' ' << squotes << str << << ' ' << dquotes << str << '\n'; should produce example 'example' "example" It is easy enough to create manipulators for changing the formatting flags themselves: std::ostream& squotes(std::ostream& out) { // what