Operators are helper functions that enable code to control input/output streams using operator<< or operator>>.
Manipulators called without arguments (such as std::cout << std::boolalpha; or std::cin >> std::hex; ) are implemented as functions that accept a reference to a stream as their only argument. Special overloaded versions of basic_ostream::operator<< and basic_istream::operator>> accept pointers to these functions. These functions (or instantiations of function templates) are the only addressable functions in the standard library. (since C++20)
Manipulators called with arguments (such as std::cout << std::setw(10); ) are implemented as functions that return an object of unspecified type. These operators define their own operator<< or operator>> that performs the requested operation.
Defined in the header file <iomanip>
Anatomy of monetary value
std::get_money
template< class MoneyT > |
(since C++11) |
When used in an expression in >> get_money(mon, intl), parses the character input for the in
monetary value specified by the std::money_get plane of the currently infected locale in , and stores the result in mon
.
The release operation in >> get_money(mon, intl) is expressed as a formatted input function (FormattedInputFunction).
parameter
mon | - | The variable to which the monetary value is to be written. Can be one of long double or basic_string |
intl | - | If true, expects to find the required international currency string, otherwise expects an optional currency symbol |
return value
Returns an unspecified type object such that if is in
the name of an input stream of type std::basic_istream<CharT, Traits>, the expression in >> get_money(mon, intl) behaves as if the following code were executed:
typedef std::istreambuf_iterator<CharT, Traits> Iter;
typedef std::money_get<CharT, Iter> MoneyGet;
std::ios_base::iostate err = std::ios_base::goodbit;
const MoneyGet &mg = std::use_facet<MoneyGet>(in.getloc());
mg.get(Iter(in.rdbuf()), Iter(), intl, in, err, mon);
if (std::ios_base::goodbit != err)
out.setstate(err);
Call example
The MinGW ports of GCC and libstdc are only supported in the "C"
and "POSIX"
environments.
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
int main()
{
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 << in.str() << " parsed as: "
<< v1 << ", " << v2 << ", " << v3 << '\n';
}
else
{
std::cout << "Parse failed";
}
return 0;
}
output
"$1,234.56 2.22 USD 3.33" parsed as: 123456, 222, 333
Format and output currency values
std::put_money
template< class MoneyT > |
(since C++11) |
When used in the expression out << put_money(mon, intl), converts the monetary value mon
into out
the character representation specified by the std::money_put plane in the currently infected locale.
The insertion operation in out << put_money(mon, intl) behaves as a formatted output function (FormattedOutputFunction).
parameter
mon | - | currency value, one of long double or std::basic_string |
intl | - | If true, use the international currency string, otherwise use the currency symbol |
return value
Returns an object of unspecified type such that if is out
the name of an output stream of type std::basic_ostream<CharT, Traits>, the expression out << put_money(mon, intl) behaves as if the following code had been executed:
typedef std::ostreambuf_iterator<CharT, Traits> Iter;
typedef std::money_put<CharT, Iter> MoneyPut;
const MoneyPut& mp = std::use_facet<MoneyPut>(out.getloc());
const Iter end = mp.put(Iter(out.rdbuf()), intl, out, out.fill(), mon);
if (end.failed())
out.setstate(std::ios::badbit);
Call example
The MinGW ports of GCC and libstdc are only supported in the "C"
and "POSIX"
environments.
#include <iostream>
#include <iomanip>
int main()
{
long double mon = 123.45; // 或 std::string mon = "123.45";
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << std::showbase
<< "en_US: " << std::put_money(mon)
<< " or " << std::put_money(mon, true) << std::endl;
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "ru_RU: " << std::put_money(mon)
<< " or " << std::put_money(mon, true) << std::endl;
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_money(mon)
<< " or " << std::put_money(mon, true) << std::endl;
return 0;
}
output
en_US: $1.23 or USD 1.23
ru_RU: 1.23 руб or 1.23 RUB
ja_JP: ¥123 or JPY 123