ITHelpself / CPlusPlus

0 stars 0 forks source link

14. Stream manipulators #15

Open ITHelpself opened 3 years ago

ITHelpself commented 3 years ago

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
ITHelpself commented 3 years ago

#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
ITHelpself commented 3 years ago

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
ITHelpself commented 3 years ago

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"
ITHelpself commented 3 years ago

#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
ITHelpself commented 3 years ago

#include <sstream>
...
std::cout << '\n'
<< "The number 0.07 in fixed: " << std::fixed << 0.01 << '\n'
<< "The number 0.07 in scientific: " << std::scientific << 0.01 << '\n'
<< "The number 0.07 in hexfloat: " << std::hexfloat << 0.01 << '\n'
<< "The number 0.07 in default: " << std::defaultfloat << 0.01 << '\n';
double f;
std::istringstream is("0x1P-1022");
double f = std::strtod(is.str().c_str(), NULL);
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
// Output:
// The number 0.01 in fixed: 0.070000
// The number 0.01 in scientific: 7.000000e-02
// The number 0.01 in hexfloat: 0x1.1eb851eb851ecp-4
// The number 0.01 in default: 0.07
// Parsing 0x1P-1022 as hex gives 2.22507e-308
Default is std::ios_base::fmtflags(0).
ITHelpself commented 3 years ago

double f;
std::istringstream("0x1P-1022") >> std::hexfloat >> f;
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
// Output: Parsing 0x1P-1022 as hex gives 
ITHelpself commented 3 years ago

std::cout << "7.0 with showpoint: " << std::showpoint << 7.0 << '\n'
<< "7.0 with noshowpoint: " << std::noshowpoint << 7.0 << '\n';
// Output: 1.0 with showpoint: 7.00000
// 1.0 with noshowpoint: 7
ITHelpself commented 3 years ago

std::cout << "With showpos: " << std::showpos
<< 0 << ' ' << -2.718 << ' ' << 17 << '\n'
<< "Without showpos: " << std::noshowpos
<< 0 << ' ' << -2.718 << ' ' << 17 << '\n';
// Output: With showpos: +0 -2.718 +17
// Without showpos: 0 -2.718 17
ITHelpself commented 3 years ago

#include <cmath>
#include <limits>
...
typedef std::numeric_limits<long double> ld;
const long double pi = std::acos(-1.L);
std::cout << '\n'
<< "default precision (6): pi: " << pi << '\n'
<< " 10pi: " << 10 * pi << '\n'
<< "std::setprecision(4): 10pi: " << std::setprecision(4) << 10 * pi << '\n'
<< " 10000pi: " << 10000 * pi << '\n'
<< "std::fixed: 10000pi: " << std::fixed << 10000 * pi << std::defaultfloat <<
'\n'
<< "std::setprecision(10): pi: " << std::setprecision(10) << pi << '\n'
<< "max-1 radix precicion: pi: " << std::setprecision(ld::digits - 1) << pi << '\n'
<< "max+1 radix precision: pi: " << std::setprecision(ld::digits + 1) << pi << '\n'
<< "significant digits prec: pi: " << std::setprecision(ld::digits10) << pi << '\n';
// Output:
// default precision (6): pi: 3.14159
// 10pi: 31.4159
// std::setprecision(4): 10pi: 31.42
// 10000pi: 3.142e+04
// std::fixed: 10000pi: 31415.9265
// std::setprecision(10): pi: 3.141592654
// max-1 radix precicion: pi: 3.14159265358979323851280895940618620443274267017841339111328125
// max+1 radix precision: pi: 3.14159265358979323851280895940618620443274267017841339111328125
// significant digits prec: pi: 3.14159265358979324
ITHelpself commented 3 years ago

#include <sstream>
...
std::istringstream in("10 010 10 010 10 010");
int num1, num2;
in >> std::oct >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::oct gives: " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives: 8 8
in >> std::dec >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::dec gives: " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives: 10 10
in >> std::resetiosflags(std::ios_base::basefield) >> num1 >> num2;
std::cout << "Parsing \"10 010\" with autodetect gives: " << num1 << ' ' << num2 << '\n';
// Parsing "10 010" with autodetect gives: 10 8
std::cout << std::setiosflags(std::ios_base::hex |
std::ios_base::uppercase |
std::ios_base::showbase) << 42 << '\n';
// Output: OX2A
ITHelpself commented 3 years ago

#include <sstream>
...
char c1, c2, c3;
std::istringstream("a b c") >> c1 >> c2 >> c3;
std::cout << "Default behavior: c1 = " << c1 << " c2 = " << c2 << " c3 = " << c3 << '\n';
std::istringstream("a b c") >> std::noskipws >> c1 >> c2 >> c3;
std::cout << "noskipws behavior: c1 = " << c1 << " c2 = " << c2 << " c3 = " << c3 << '\n';
// Output: Default behavior: c1 = a c2 = b c3 = c
// noskipws behavior: c1 = a c2 = c3 = b
ITHelpself commented 3 years ago

#include <sstream>
...
std::stringstream ss;
std::string in = "String with spaces, and embedded \"quotes\" too";
std::string out;
ss << std::quoted(in);
std::cout << "read in [" << in << "]\n"
<< "stored as [" << ss.str() << "]\n";
ss >> std::quoted(out);
std::cout << "written out [" << out << "]\n";
// Output:
// read in [String with spaces, and embedded "quotes" too]
// stored as ["String with spaces, and embedded \"quotes\" too"]
// written out [String with spaces, and embedded "quotes" too]
ITHelpself commented 3 years ago

std::cout << "First line." << std::endl << "Second line. " << std::flush
<< "Still second line.";
ITHelpself commented 3 years ago

std::cout << "\nDefault fill: " << std::setw(10) << 79 << '\n'
<< "setfill('#'): " << std::setfill('#')
<< std::setw(10) << 42 << '\n';
// Output:
// Default fill: 79
// setfill('#'): ########79
ITHelpself commented 3 years ago

long double money = 123.45;
// or std::string money = "123.45";
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << std::showbase << "en_US: " << std::put_money(money)
<< " or " << std::put_money(money, true) << '\n';
// Output: en_US: $1.23 or USD 1.23
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "ru_RU: " << std::put_money(money)
<< " or " << std::put_money(money, true) << '\n';
// Output: ru_RU: 1.23 руб or 1.23 RUB
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_money(money)
<< " or " << std::put_money(money, true) << '\n';
// Output: ja_JP: ¥123 or JPY 123
ITHelpself commented 3 years ago

#include <ctime>
...
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "\nru_RU: " << std::put_time(&tm, "%c %Z") << '\n';
// Possible output:
// ru_RU: Вт 04 июл 2017 15:08:35 UTC
ITHelpself commented 3 years ago

#include <sstream>
...
std::string str;
std::istringstream(" \v\n\r\t Wow!There is no whitespaces!") >> std::ws >> str;
std::cout << str;
// Output: Wow!There is no whitespaces!
ITHelpself commented 3 years ago

#include <sstream>
#include <locale>
...
std::istringstream in("$1,234.56 2.22 USD 3.33");
long double v1, v2;
std::string v3;
in.imbue(std::locale("en_US.UTF-8"));
in >> std::get_money(v1) >> std::get_money(v2) >> std::get_money(v3, true);
if (in) {
std::cout << std::quoted(in.str()) << " parsed as: "
<< v1 << ", " << v2 << ", " << v3 << '\n';
}
// Output:
// "$1,234.56 2.22 USD 3.33" parsed as: 123456, 222, 333
ITHelpself commented 3 years ago

#include <sstream>
#include <locale>
...
std::tm t = {};
std::istringstream ss("2011-Februar-18 23:12:34");
ss.imbue(std::locale("de_DE.utf-8"));
ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
if (ss.fail()) {
std::cout << "Parse failed\n";
}
else {
std::cout << std::put_time(&t, "%c") << '\n';
}
// Possible output:
// Sun Feb 18 23:12:34 2011