Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::num_get

      From cppreference.com
      <cpp‎ |locale
       
       
       
      Localization library
       
       
      Defined in header<locale>
      template<

         class CharT,
         class InputIt=std::istreambuf_iterator<CharT>

      >class num_get;

      Classstd::num_get encapsulates the rules for parsing string representations of numeric values. Specifically, typesbool,unsignedshort,unsignedint,long,unsignedlong,longlong,unsignedlonglong(since C++11),float,double,longdouble, andvoid* are supported. The standard formatting input operators (such ascin>> n;) use thestd::num_get facet of the I/O stream's locale to parse the text representations of the numbers.

      std-num get-inheritance.svg

      If astd::num_get specialization is not guaranteed to be provided by the standard library (see below), the behaviors of itsget() anddo_get() are not guaranteed as specified.

      Contents

      [edit]Specializations

      The standard library is guaranteed to provide the following specializations (they arerequired to be implemented by any locale object):

      Defined in header<locale>
      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:

      [edit]Nested types

      Type Definition
      char_typeCharT
      iter_typeInputIt

      [edit]Data members

      Member Description
      std::locale::idid[static] the identifier of thefacet

      [edit]Member functions

      constructs a newnum_get facet
      (public member function)
      invokesdo_get
      (public member function)

      [edit]Protected member functions

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

      [edit]Example

      Run this code
      #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 streamsstd::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 directlystd::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_USparsing 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.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 427C++98num_get was guaranteed to accept anyCharT that
      meets the requirements for a character on which
      any of the iostream components can be instantiated
      only guarantees to acceptchar,
      wchar_t and other implementation-
      defined character types
      LWG 2392C++98only character typeCharT could be
      guaranteed to be accepted bynum_get
      can guarantee to accept implementation-
      defined character container types

      [edit]See also

      defines numeric punctuation rules
      (class template)[edit]
      formats numeric values for output as character sequence
      (class template)[edit]
      extracts formatted data
      (public member function ofstd::basic_istream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/num_get&oldid=179024"

      [8]ページ先頭

      ©2009-2025 Movatter.jp