Class std::num_get
encapsulates the rules for parsing string representations of numeric values. Specifically, types bool, unsigned short, unsigned int, long, unsigned long, long long, unsigned long long(since C++11), float, double, long double, and void* are supported. The standard formatting input operators (such as cin >> n;) use the std::num_get
facet of the I/O stream's locale to parse the text representations of the numbers.
Inheritance diagram
If a std::num_get
specialization is not guaranteed to be provided by the standard library (see below), the behaviors of its get() and do_get() are not guaranteed as specified.
The standard library is guaranteed to provide the following specializations (they are required to be implemented by any locale object):
std::num_get<char> creates narrow string parsing of numbers std::num_get<wchar_t> creates wide string parsing of numbersIn addition, the standard library is also guaranteed to provide every specialization that satisfies the following type requirements:
CharT
is one of
InputIt
must meet the requirements of LegacyInputIterator.char_type
CharT
iter_type
InputIt
[edit] Data members [edit] Member functions constructs a new num_get
facet
do_get
num_get
facet
#include <iostream> #include <iterator> #include <locale> #include <sstream> #include <string> int main() { std::string de_double = "1.234.567,89"; std::string us_double = "1,234,567.89"; // parse using streams std::istringstream de_in(de_double); de_in.imbue(std::locale("de_DE.UTF-8")); double f1; de_in >> f1; std::istringstream us_in(de_double); us_in.imbue(std::locale("en_US.UTF-8")); double f2; us_in >> f2; std::cout << "Parsing " << de_double << " as double gives " << std::fixed << f1 << " in de_DE locale and " << f2 << " in en_US\n"; // use the facet directly std::istringstream s3(us_double); s3.imbue(std::locale("en_US.UTF-8")); auto& f = std::use_facet<std::num_get<char>>(s3.getloc()); std::istreambuf_iterator<char> beg(s3), end; double f3; std::ios::iostate err; f.get(beg, end, s3, err, f3); std::cout << "parsing " << us_double << " as double using raw en_US facet gives " << f3 << '\n'; }
Output:
Parsing 1.234.567,89 as double gives 1234567.890000 in de_DE locale and 1.234000 in en_US parsing 1,234,567.89 as double using raw en_US facet gives 1234567.890000[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 427 C++98num_get
was guaranteed to accept any CharT
that
CharT
could be
num_get
can guarantee to accept implementation-
std::basic_istream<CharT,Traits>
) [edit]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4