[std::boolalpha](<http://en.cppreference.com/w/cpp/io/manip/booalpha>)
and [std::noboolalpha](<http://en.cppreference.com/w/cpp/io/manip/booalpha>)
- switch between textual and numeric representation of booleans.
std::cout << std::boolalpha << 1;
// Output: true
std::cout << std::noboolalpha << false;
// Output: 0
bool boolValue;
std::cin >> std::boolalpha >> boolValue;
std::cout << "Value \\"" << std::boolalpha << boolValue
<< "\\" was parsed as " << std::noboolalpha << boolValue;
// Input: true
// Output: Value "true" was parsed as 0
[std::showbase](<http://en.cppreference.com/w/cpp/io/manip/showbase>)
and [std::noshowbase](<http://en.cppreference.com/w/cpp/io/manip/showbase>)
- control whether prefix indicating numeric base is used.
[std::dec](<http://en.cppreference.com/w/cpp/io/manip/hex>)
(decimal), [std::hex](<http://en.cppreference.com/w/cpp/io/manip/hex>)
(hexadecimal) and [std::oct](<http://en.cppreference.com/w/cpp/io/manip/hex>)
(octal) - are used for changing base for integers.
#include <sstream>
std::cout << std::dec << 29 << ' - '
<< std::hex << 29 << ' - '
<< std::showbase << std::oct << 29 << ' - '
<< std::noshowbase << 29 '\\n';
int number;
std::istringstream("3B") >> std::hex >> number;
std::cout << std::dec << 10;
// Output: 22 - 1D - 35 - 035
// 59
Default values are [std::ios_base::noshowbase](<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>)
and [std::ios_base::dec](<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>)
.
If you want to see more about std::istringstream
check out the <sstream> header.
[std::uppercase](<http://en.cppreference.com/w/cpp/io/manip/uppercase>)
and [std::nouppercase](<http://en.cppreference.com/w/cpp/io/manip/uppercase>)
- control whether uppercase characters are used in floating-point and hexadecimal integer output. Have no effect on input streams.
std::cout << std::hex << std::showbase
<< "0x2a with nouppercase: " << std::nouppercase << 0x2a << '\\n'
<< "1e-10 with uppercase: " << std::uppercase << 1e-10 << '\\n'
}
// Output: 0x2a with nouppercase: 0x2a
// 1e-10 with uppercase: 1E-10
Default is std::nouppercase
.
[std::setw(n)](<http://en.cppreference.com/w/cpp/io/manip/setw>)
- changes the width of the next input/output field to exactly n
.
The width property n
is resetting to 0
when some functions are called (full list is here).
std::cout << "no setw:" << 51 << '\\n'
<< "setw(7): " << std::setw(7) << 51 << '\\n'
<< "setw(7), more output: " << 13
<< std::setw(7) << std::setfill('*') << 67 << ' ' << 94 << '\\n';
char* input = "Hello, world!";
char arr[10];
std::cin >> std::setw(6) >> arr;
std::cout << "Input from \\"Hello, world!\\" with setw(6) gave \\"" << arr << "\\"\\n";
// Output: 51
// setw(7): 51
// setw(7), more output: 13*****67 94
// Input: Hello, world!
// Output: Input from "Hello, world!" with setw(6) gave "Hello"
Default is std::setw(0)
.
[std::left](<http://en.cppreference.com/w/cpp/io/manip/left>)
, [std::right](<http://en.cppreference.com/w/cpp/io/manip/left>)
and [std::internal](<http://en.cppreference.com/w/cpp/io/manip/left>)
- modify the default position of the fill characters by setting [std::ios_base::adjustfield](<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>)
to [std::ios_base::left](<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>)
, [std::ios_base::right](<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>)
and [std::ios_base::internal](<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>)
correspondingly. std::left
and std::right
apply to any output, std::internal
- for integer, floating-point and monetary output. Have no effect on input streams.
#include <locale>
...
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << std::left << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\\n'
<< "hex: " << std::setw(15) << 41 << '\\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, false) << '\\n';
// Output:
// flt: -9.87**********
// hex: 41*************
// $: $3.67**********
// usd: USD *3.67******
// usd: $3.67
std::cout << std::internal << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\\n'
<< "hex: " << std::setw(15) << 41 << '\\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, true) << '\\n';
// Output:
// flt: -**********9.87
// hex: *************41
// $: $3.67**********
// usd: USD *******3.67
// usd: USD 3.67
std::cout << std::right << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\\n'
<< "hex: " << std::setw(15) << 41 << '\\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, true) << '\\n';
// Output:
// flt: **********-9.87
// hex: *************41
// $: **********$3.67
// usd: ******USD *3.67
// usd: USD 3.67
Default is std::left
.
[std::fixed](<http://en.cppreference.com/w/cpp/io/manip/fixed>)
, [std::scientific](<http://en.cppreference.com/w/cpp/io/manip/fixed>)
, [std::hexfloat](<http://en.cppreference.com/w/cpp/io/manip/fixed>)
[C++11] and [std::defaultfloat](<http://en.cppreference.com/w/cpp/io/manip/fixed>)
[C++11] - change formatting for floating-point input/output.
std::fixed
sets the [std::ios_base::floatfield](<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>)
to [std::ios_base::fixed](<http://en.cppreference.com/w/cpp/io/ios_base/fmtflags>)
,