Questions tagged [ostream]

In C++ std::ostream is the base class for output streams.

765 questions
280
votes
6 answers

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

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…
105
votes
9 answers

How to make C++ cout not use scientific notation

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: "<
Yunus Eren Güzel
  • 3,018
  • 11
  • 36
  • 63
85
votes
17 answers

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

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…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
84
votes
5 answers

overloading friend operator<< for template class

I have read couple of the questions regarding my problem on StackOverflow.com now, and none of it seems to solve my problem. Or I maybe have done it wrong... The overloaded << works if I make it into an inline function. But how do I make it work in…
starcorn
  • 8,261
  • 23
  • 83
  • 124
70
votes
9 answers

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

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?
paperjam
  • 8,321
  • 12
  • 53
  • 79
59
votes
9 answers

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

I am learning C++. cout is an instance of std::ostream class. How can I print a formatted string with it? I can still use printf, but I want to learn a proper C++ method which can take advantage of all C++ benefits. I think this should be possible…
eonil
  • 83,476
  • 81
  • 317
  • 516
54
votes
8 answers

Floating point format for std::ostream

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…
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
46
votes
6 answers

How to inherit from std::ostream?

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…
Michael
  • 2,826
  • 4
  • 25
  • 18
46
votes
5 answers

Why is istream/ostream slow

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…
user34537
38
votes
2 answers

Why is operator<< function between std::ostream and char a non-member function?

When I ran the following program #include int main() { char c = 'a'; std::cout << c << std::endl; std::cout.operator<<(c) << std::endl; return 0; } I got the output a 97 Digging further at…
R Sahu
  • 204,454
  • 14
  • 159
  • 270
25
votes
3 answers

How do I create my own ostream/streambuf?

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…
user34537
24
votes
3 answers

How do the stream manipulators work?

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'<
Narek
  • 38,779
  • 79
  • 233
  • 389
24
votes
6 answers

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

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…
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
21
votes
3 answers

Platform independent /dev/null in c++

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…
calccrypto
  • 8,583
  • 21
  • 68
  • 99
20
votes
3 answers

Does Overloading Operator<< works inside the class?

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);…
howtechstuffworks
  • 1,824
  • 4
  • 29
  • 46
1
2 3
50 51