Questions tagged [iomanip]

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.

164 questions
67
votes
9 answers

How can I print 0x0a instead of 0xa using cout?

How can I print 0x0a, instead of 0xa using cout? #include using std::cout; using std::endl; using std::hex; int main() { cout << hex << showbase << 10 << endl; }
Ayrosa
  • 3,089
  • 1
  • 14
  • 28
32
votes
2 answers

Decimal points with std::stringstream?

I have a bunch of integers that I put into stringstreams. Now I want to change the stringstreams into strings while keeping a constant precision with the strings. How would I do that? I know I can use stringstreams.precision(), but it's not working…
noobcpp
  • 321
  • 1
  • 3
  • 3
15
votes
4 answers

How does "std::cout << std::endl;" compile?

Most IO stream manipulators are regular functions with the following signature: std::ios_base& func( std::ios_base& str ); However some manipulators (including the most frequently used ones - std::endl and std::flush) are templates of the following…
Leon
  • 28,052
  • 3
  • 52
  • 82
14
votes
3 answers

How do I use iomanip's setw, setfill, and left/right? Setfill isn't stopping its output

I'm trying to get my output to look like this: size time1 time2 ------------------------------- 10 4 8 100 48 16 1000 2937 922 10000 123011 3902 100000 22407380 830722 And…
user3448821
  • 219
  • 1
  • 2
  • 12
14
votes
2 answers

Correctly pad negative integers with zeros with std::cout

I found this question already asked, but the answer everybody gives is std::cout << std::setw(5) << std::setfill('0') << value << std::endl; which is fine for positive numbers, but with -5, it prints: 000-5 Is there a way to make it print -0005 or…
Philippe
  • 1,137
  • 9
  • 19
13
votes
5 answers

cout << setw doesn't align correctly with åäö

The following code reproduces my problem: #include #include #include void p(std::string s, int w) { std::cout << std::left << std::setw(w) << s; } int main(int argc, char const *argv[]) { p("COL_A", 7); …
Gestur
  • 133
  • 4
11
votes
2 answers

How can I stream hexadecimal numbers with A-F (rather than a-f)?

Is it possible to make ostream output hexadecimal numbers with characters A-F and not a-f? int x = 0xABC; std::cout << std::hex << x << std::endl; This outputs abc whereas I would prefer to see ABC.
Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
10
votes
4 answers

What's the opposite of `fixed` in cout?

When using cout, what is the default formatter defined in the header? In other words, once I've set my formatter to fixed using cout << fixed << setPrecision(2), how do I change it back? Or, what am I changing it back to?
Moshe
  • 55,729
  • 73
  • 263
  • 420
10
votes
2 answers

Can `std::istream::operator>>()` accept integer radix prefixes like stdio's %i format specifier?

When using scanf() and its variants, the format specifier %i will accept data as hex (prefixed "0x"), octal (prefixed "0"), or decimal (no prefix), so for example the strings "0x10", "020", and "16" are all converted to an integer with decimal value…
Clifford
  • 76,825
  • 12
  • 79
  • 145
10
votes
4 answers

Can you pass a manipulator to a function?

I'd like to pass a list of manipulators to a function, something like this: void print(const vector& manips) { // ... for (auto m : manips) cout << m; // ... } which would ideally be called by code something like…
Ben Kovitz
  • 4,279
  • 1
  • 17
  • 40
9
votes
4 answers

Effective use of C++ iomanip library

I created a Vector class in C++ and it works great for my problems. I am now cleaning it up, and I ran into the following piece of code: std::ostream& operator<<(std::ostream &output, const Vector &v){ output<<"[" …
Escualo
  • 36,702
  • 18
  • 79
  • 122
9
votes
1 answer

How are iomanip functions Implemented?

Some of the standard iomanip functions take take a parameter. I'd like to know how this is accomplished, for instance, can I do something similar with a function? That's really the solution that I needed for this answer, but I couldn't figure out…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
8
votes
3 answers

Does put_money hold its argument by value or reference?

Does the following invoke undefined behavior? #include #include #include #include int main() { long double values[] = {1, 2, 3}; std::transform( std::begin(values),…
Eric
  • 87,154
  • 48
  • 211
  • 332
8
votes
4 answers

What is the role of **std::setprecision()** without **std::fixed** in c++?

As shown in the tutorial http://www.cplusplus.com/reference/iomanip/setprecision/ // setprecision example #include // std::cout, std::fixed #include // std::setprecision int main () { double f =3.14159; std::cout…
Talespin_Kit
  • 17,730
  • 25
  • 86
  • 117
7
votes
4 answers

Formatting floats: returning to default

I am running into a formatting issue on floating-point values, in the sense of returning to "default formatting". Say I have 2 floats: float f1 = 3.0f, f2 = 1.5f; std::cout << f1 << " - " << f2 << "\n"; will show these as: 3 - 1.5 Now, for some…
kebs
  • 4,983
  • 3
  • 36
  • 61
1
2 3
10 11