Questions tagged [manipulators]

Anything related to C++ I/O manipulators, i.e. special kinds of objects that alter the behavior of streams. Inserting a manipulator into an output stream or extracting one from an input stream provides an easy alternative for configuring specific aspects of the stream operations.

Anything related to C++ I/O manipulators, i.e. special kinds of objects that alter the behavior of streams. Inserting a manipulator into an output stream or extracting one from an input stream provides an easy alternative for configuring specific aspects of the stream operations.

Simple manipulators are defined directly in the C++ standard header <iostream>. Manipulators taking arguments (i.e. functions that return actual manipulator objects) are defined in the <iomanip> standard header.

See CPPreference.com documentation for I/O manipulators.

87 questions
37
votes
7 answers

Overload handling of std::endl?

I want to define a class MyStream so that: MyStream myStream; myStream << 1 << 2 << 3 << std::endl << 5 << 6 << std::endl << 7 << 8 << std::endl; gives output [blah]123 [blah]56 [blah]78 Basically, I want a "[blah]" inserted at the front, then…
anon
  • 41,035
  • 53
  • 197
  • 293
27
votes
5 answers

C++ - How to reset the output stream manipulator flags

I've got a line of code that sets the fill value to a '-' character in my output, but need to reset the setfill flag to its default whitespace character. How do I do that? cout << setw(14) << " CHARGE/ROOM" << endl; cout << setfill('-') << setw(11)…
noobzilla
  • 1,095
  • 2
  • 11
  • 17
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
12
votes
3 answers

How do I use a manipulator to format my hex output with padded left zeros

The little test program below prints out: And SS Number IS =3039 I would like the number to print out with padded left zeros such that the total length is 8. So: And SS Number IS =00003039 (notice the extra zeros left padded) And I would…
bbazso
  • 1,959
  • 6
  • 22
  • 30
11
votes
3 answers

std::function as a custom stream manipulator

I'm trying to use C++11 features to make custom stream manipulators easier to create. I can use lambda functions as manipulators, but not std::function. Here's the code, boiled down: #include #include…
Ed Krohne
  • 334
  • 1
  • 11
10
votes
2 answers

ostream showbase does not show "0x" for zero value

PSPS: (a Pre-scripted Post-script) It has just come to mind that a more prescient question would have included the notion of: Is this non-display of "0x"(showbase) for zero-value integers a standard behaviour, or is it just a quirk of my MinGW…
Peter.O
  • 6,696
  • 4
  • 30
  • 37
10
votes
2 answers

Should std::ws raise failbit at end of file?

Should extracting from a stream using the std::ws manipulator ever raise the fail bit? In the following code, a Clang-compiled (within Xcode 4.5.1) program fails the final assertion. Evidently s >> std::ws at EOF causes a fail. Yet GCC 4.7.2 passes…
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
9
votes
2 answers

c++ custom output stream with indentation

I'm having some trouble trying to implement a custom stream class to generate nicely indented code in an output file. I've searched online extensively but there doesn't seem to be a consensus on the best way to achieve this. Some people talk about…
user1898153
  • 463
  • 6
  • 12
8
votes
2 answers

How do stream manipulators with arguments work?

In Stroustrup's C++ book, there is an example of a custom manipulator taking an argument (pls see the attached code). I am confused about how the struct is created. In particular, it looks like there are two int arguments for the constructor of…
ubbdd
  • 241
  • 2
  • 8
8
votes
1 answer

Effect of noskipws on cin>>

As I understand, the extraction operator skips the whitespace in the beginning and stops upon encountering a whitespace or end of stream. noskipws can be used to stop ignoring the leading whitespaces. I have the following program where I have used…
Achint Mehta
  • 349
  • 1
  • 5
  • 14
6
votes
2 answers

How to Generically Define Insertion Operators for all C++ IOStream Manipulators?

All, Why does the following code fail to compile for 'std::endl', but it's fine for all of the other inserted types? #include // ostringstream /// @brief A class that does streamed, formatted output via 'operator<<'. class…
Charles L Wilcox
  • 1,126
  • 8
  • 18
6
votes
2 answers

How to use this manipulator

This is a exercise for school, so please provide just hints and no complete examples ;-) I have my own manipulator: template > ios_base& toggle(basic_ios& io) { if(io.flags() & ios::scientific) {…
Michael
  • 706
  • 9
  • 29
5
votes
1 answer

How to put our own function declaration in iostream library in c++?

ostream& tab (ostream &o) { return o << '\t'; } I want to put this declaration in iostream library..how can i do this??
shubhendu mahajan
  • 816
  • 1
  • 7
  • 16
5
votes
4 answers

How to write my own manipulator?

Let's suppose I want to write my own manipulator for input and output. cin >> mymanip >> str; or cout << mymanip << str; What I want that mymanip does is toggle case the caracters I read from input and assigns the result to one string. So, if I…
user7140484
  • 920
  • 6
  • 14
5
votes
2 answers

Template type deduction for stream manipulators

I'm unsure as to whether this code will not compile. The example code I'm working with: #include using std::cout; using std::endl; class Foo { public: template Foo& operator<<(const T& t) { …
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
1
2 3 4 5 6