From cppreference.com
template<classCharT,classInputIt=std::istreambuf_iterator<CharT>>classnum_get;Class std::num_get encapsulates the rules for parsing string representations of numeric values. Specifically, types bool, unsignedshort, unsignedint, long, unsignedlong, longlong, unsignedlonglong(since C++11), float, double, longdouble, 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
and
are not guaranteed as specified.
Specializations
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 numbers In addition, the standard library is also guaranteed to provide every specialization that satisfies the following type requirements:
CharT is one of char,
wchar_t, and
any other implementation-defined
that meets the requirements for a character on which any of the
can be instantiated; and
InputIt must meet the requirements of
.
Nested types
Type Definition char_typeCharTiter_typeInputItData members
Member functions
constructs a new num_get facet
(public member function)invokes do_get
(public member function)Protected member functions
destructs a num_get facet
(protected member function)[virtual]
parses a number from an input stream
(virtual protected member function)Example
Run this code
#include<iostream>#include<iterator>#include<locale>#include<sstream>#include<string>intmain(){std::stringde_double="1.234.567,89";std::stringus_double="1,234,567.89";// parse using streamsstd::istringstreamde_in(de_double);de_in.imbue(std::locale("de_DE.UTF-8"));doublef1;de_in>>f1;std::istringstreamus_in(de_double);us_in.imbue(std::locale("en_US.UTF-8"));doublef2;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 directlystd::istringstreams3(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;doublef3;std::ios::iostateerr;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 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 num_get was guaranteed to accept any CharT that
meets the requirements for a character on which
any of the iostream components can be instantiated only guarantees to accept char,
wchar_t and other implementation-
defined character types
C++98 only character type CharT could be
guaranteed to be accepted by num_getcan guarantee to accept implementation-
defined character container types See also
defines numeric punctuation rules
(class template)
formats numeric values for output as character sequence
(class template)
extracts formatted data
(public member function of std::basic_istream<CharT,Traits>)