Input stream manipulators

suggest change

std::ws consumes leading whitespace in an input stream. It is different from std::skipws, which is a format flag. operator>> for std::string then stops at the next whitespace, so this extraction reads only Wow!There.

#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

std::get_money(mon[, intl]) [C++11]. In an expression in >> std::get_money(mon, intl) parses the character input as a monetary value, as specified by the std::money_get facet of the locale currently imbued in in, and stores the value in mon (of long double or std::basic_string type). Manipulator expects required international currency strings if intl is true, expects optional currency symbols otherwise.

#include <sstream>
#include <locale>
#include <iomanip>
...
 
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

std::get_time(tmb, fmt) [C++11] parses a date/time from the input according to fmt and stores it in *tmb. tmb is a pointer to a non-const std::tm. fmt is a pointer to a null-terminated string (const CharT*) specifying the conversion format. The locale names below have to be installed, and the abbreviated month name comes from that locale.

#include <sstream>
#include <locale>
#include <iomanip>
...

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 << t.tm_year + 1900 << '-'
              << t.tm_mon + 1 << '-'
              << t.tm_mday << ' '
              << std::put_time(&t, "%H:%M:%S") << '\n';
}
// Output:
// 2011-2-18 23:12:34

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents