Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/num get"

From cppreference.com
< cpp‎ | locale
m (Text replace - "{{cpp|" to "{{c|")
m (Text replace - "{{tdcl list begin" to "{{dcl list begin")
Line 14: Line 14:
 
Two specializations and two partial specializations are provided by the standard library and are implemented by all locale objects created in a C++ program:
 
Two specializations and two partial specializations are provided by the standard library and are implemented by all locale objects created in a C++ program:
  
{{tdcl list begin}}
+
{{dcl list begin}}
 
{{tdcl list header | locale }}
 
{{tdcl list header | locale }}
 
{{tdcl list item | {{c|std::num_get<char>}} | creates narrow string parsing of numbers}}
 
{{tdcl list item | {{c|std::num_get<char>}} | creates narrow string parsing of numbers}}
Line 23: Line 23:
  
 
===Member types===
 
===Member types===
{{tdcl list begin}}
+
{{dcl list begin}}
 
{{tdcl list hitem | Member type | Definition}}
 
{{tdcl list hitem | Member type | Definition}}
 
{{tdcl list item | {{tt|char_type}} | {{tt|charT}}}}
 
{{tdcl list item | {{tt|char_type}} | {{tt|charT}}}}
Line 30: Line 30:
  
 
===Member objects===
 
===Member objects===
{{tdcl list begin}}
+
{{dcl list begin}}
 
{{tdcl list hitem | Member name | Type}}
 
{{tdcl list hitem | Member name | Type}}
 
{{tdcl list item | {{tt|id}} {{mark|static}} | {{c|std::locale::id}} }}
 
{{tdcl list item | {{tt|id}} {{mark|static}} | {{c|std::locale::id}} }}

Revision as of 01:16, 12 June 2012

Template:cpp/locale/num get/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <locale>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class charT,

          class InputIterator = std::istreambuf_iterator<charT>

> class num_get : public std::locale::facet;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Class std::num_get encapsulates the rules for parsing string representations of values of type bool, unsigned short, unsigned int, long, unsigned long, long long, unsigned long long, float, double, long double, and void*. 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.

Two specializations and two partial specializations are provided by the standard library and are implemented by all locale objects created in a C++ program:

Template:tdcl list headerTemplate:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list end

Contents

Member types

Template:tdcl list hitemTemplate:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list end

Member objects

Template:tdcl list hitemTemplate:tdcl list itemTemplate:tdcl list end

Member functions

constructs a new num_get facet
(public member function)
destructs a num_get facet
(protected member function)
invokes do_get
(public member function)

Protected member functions

[virtual]
parses a number from an input stream
(virtual protected member function)

Example

#include <iostream>
#include <locale>
#include <string>
#include <sstream>
#include <iterator>
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"));
    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

See also

Template:cpp/locale/dcl list numpunctTemplate:cpp/locale/dcl list num putTemplate:cpp/io/basic istream/dcl list operator gtgt