| Localization library | |||||||||||||||||||||||||
| Regular expressions library(C++11) | |||||||||||||||||||||||||
| Formatting library(C++20) | |||||||||||||||||||||||||
| Null-terminated sequence utilities | |||||||||||||||||||||||||
| Byte strings | |||||||||||||||||||||||||
| Multibyte strings | |||||||||||||||||||||||||
| Wide strings | |||||||||||||||||||||||||
| Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
| Text encoding identifications | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <locale> | ||
template< class CharT, | ||
Classstd::num_put encapsulates the rules for formatting numeric values as strings. Specifically, the typesbool,long,unsignedlong,longlong,unsignedlonglong(since C++11),double,longdouble,void*, and of all types implicitly convertible to these (such asint orfloat) are supported. The standard formatting output operators (such ascout<< n;) use thestd::num_put facet of the I/O stream's locale to generate text representation of numbers.
Inheritance diagram
If astd::num_put specialization is not guaranteed to be provided by the standard library (see below), the behaviors of itsput() anddo_put() are not guaranteed as specified.
Contents |
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_put<char> | creates narrow string representations of numbers |
| std::num_put<wchar_t> | creates wide string representations of numbers |
In addition, the standard library is also guaranteed to provide every specialization that satisfies the following type requirements:
CharT is one ofOutputIt must meet the requirements ofLegacyOutputIterator.| Type | Definition |
char_type | CharT |
iter_type | OutputIt |
| Member | Description |
std::locale::idid[static] | the identifier of thefacet |
constructs a newnum_put facet(public member function) | |
invokesdo_put(public member function) |
destructs anum_put facet(protected member function) | |
[virtual] | formats a number and writes to output stream (virtual protected member function) |
#include <iostream>#include <iterator>#include <locale>#include <string> int main(){double n=1234567.89;std::cout.imbue(std::locale("de_DE.UTF-8"));std::cout<<"Direct conversion to string:\n"<<std::to_string(n)<<'\n'<<"Output using a german locale:\n"<<std::fixed<< n<<'\n'<<"Output using an american locale:\n"; // use the facet directlystd::cout.imbue(std::locale("en_US.UTF-8"));auto& f=std::use_facet<std::num_put<char>>(std::cout.getloc()); f.put(std::ostreambuf_iterator<char>(std::cout),std::cout,' ', n);std::cout<<'\n';}
Possible output:
Direct conversion to string:1234567.890000Output using a german locale:1.234.567,890000Output using an american locale:1,234,567.890000
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++98 | num_put was guaranteed to accept anyCharT thatmeets 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 2392 | C++98 | only character typeCharT could beguaranteed to be accepted by num_put | can guarantee to accept implementation- defined character container types |
| defines numeric punctuation rules (class template)[edit] | |
| parses numeric values from an input character sequence (class template)[edit] | |
(C++11) | converts an integral or floating-point value tostring(function)[edit] |
(C++11) | converts an integral or floating-point value towstring(function)[edit] |