std::get_money
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
Input/output manipulators
Floating-point formatting
(C++11)(C++11)
Integer formatting
Boolean formatting
Field width and fill control
Other formatting
Whitespace processing
Output flushing
(C++20)(C++20)
Status flags manipulation
Time and money I/O
Quoted manipulator
(C++14)
Defined in header
<iomanip>
template< class MoneyT >
/*unspecified*/ get_money( MoneyT& mon, bool intl = false );
(since C++11)
/*unspecified*/ get_money( MoneyT& mon, bool intl = false );
When used in an expression in >> 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.
The extraction operation in in >> get_money(mon, intl) behaves as a FormattedInputFunction.
Contents
[edit] Parameters
mon
-
variable where monetary value will be written. Can be either long double or std::basic_string
intl
-
expects to find required international currency strings if true, expects optional currency symbols otherwise
[edit] Return value
An object of unspecified type such that
- if in is an object of type std::basic_istream <CharT, Traits>, the expression in >> get_money(mon, intl)
- has type std::basic_istream <CharT, Traits>&
- has value in
- behaves as if it called f(in, mon, intl)
where the function f is defined as:
template<class CharT, class Traits, class MoneyT> void f(std::basic_ios <CharT, Traits>& str, MoneyT& mon, bool intl) { using Iter = std::istreambuf_iterator <CharT, Traits>; using MoneyGet = std::money_get <CharT, Iter>; std::ios_base::iostate err = std::ios_base::goodbit ; const MoneyGet& mg = std::use_facet <MoneyGet>(str.getloc()); mg.get(Iter(str.rdbuf()), Iter(), intl, str, err, mon); if (err != std::ios_base::goodbit ) str.setstate(err); }
[edit] Example
Run this code
#include <iomanip> #include <iostream> #include <locale> #include <sstream> 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 << std::quoted (in.str()) << " parsed as: " << v1 << ", " << v2 << ", " << v3 << '\n'; else std::cout << "Parse failed"; }
Output:
"1,234ドル.56 2.22 USD 3.33" parsed as: 123456, 222, 333