std::boolalpha
and std::noboolalpha
- 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
and std::noshowbase
- control whether prefix indicating numeric base is used.
std::dec
(decimal), std::hex
(hexadecimal) and std::oct
(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
and std::ios_base::dec
.
If you want to see more about std::istringstream
check out the <sstream> header.
std::uppercase
and std::nouppercase
- 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)
- 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
, std::right
and std::internal
- modify the default position of the fill characters by setting std::ios_base::adjustfield
to std::ios_base::left
, std::ios_base::right
and std::ios_base::internal
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
, std::scientific
, std::hexfloat
[C++11] and std::defaultfloat
[C++11] - change formatting for floating-point input/output.
std::fixed
sets the std::ios_base::floatfield
to std::ios_base::fixed
,