| 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 | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
num_put::putnum_put::do_put |
Defined in header <locale> | ||
| (1) | ||
public: iter_type put( iter_type out,std::ios_base& str, | ||
iter_type put( iter_type out,std::ios_base& str, char_type fill,long val)const; | ||
iter_type put( iter_type out,std::ios_base& str, char_type fill,longlong val)const; | (since C++11) | |
iter_type put( iter_type out,std::ios_base& str, char_type fill,unsignedlong val)const; | ||
iter_type put( iter_type out,std::ios_base& str, char_type fill,unsignedlonglong val)const; | (since C++11) | |
iter_type put( iter_type out,std::ios_base& str, char_type fill,double val)const; | ||
iter_type put( iter_type out,std::ios_base& str, char_type fill,longdouble val)const; | ||
iter_type put( iter_type out,std::ios_base& str, char_type fill,constvoid* val)const; | ||
| (2) | ||
protected: virtual iter_type do_put( iter_type out,std::ios_base& str, | ||
virtual iter_type do_put( iter_type out,std::ios_base& str, char_type fill,long val)const; | ||
virtual iter_type do_put( iter_type out,std::ios_base& str, char_type fill,longlong val)const; | (since C++11) | |
virtual iter_type do_put( iter_type out,std::ios_base& str, char_type fill,unsignedlong val)const; | ||
virtual iter_type do_put( iter_type out,std::ios_base& str, char_type fill,unsignedlonglong val)const; | (since C++11) | |
virtual iter_type do_put( iter_type out,std::ios_base& str, char_type fill,double val)const; | ||
virtual iter_type do_put( iter_type out,std::ios_base& str, char_type fill,longdouble val)const; | ||
virtual iter_type do_put( iter_type out,std::ios_base& str, char_type fill,constvoid* val)const; | ||
do_put of the most derived class.Conversion occurs in four stages:
Contents |
| (until C++11) |
| (since C++11) |
| (since C++11) |
CharT by callingstd::use_facet<std::ctype<CharT>>(str.getloc()).widen(c).CharT's after Stage 2 is less thanstr.width(), then copies of thefill character are inserted at the position indicated by padding to bring the length of the sequence tostr.width().In any case,str.width(0) is called to cancel the effects ofstd::setw.
Every successive characterc from the sequence ofCharT's from Stage 3 is output as if by*out++= c.
| out | - | iterator pointing to the first character to be overwritten |
| str | - | stream to retrieve the formatting information from |
| fill | - | padding character used when the results needs to be padded to the field width |
| val | - | value to convert to string and output |
out
The leading zero generated by the conversion specification#o (resulting from the combination ofstd::showbase andstd::oct for example) is not counted as a padding character.
When formatting a floating point value as hexfloat (i.e., whenfloatfield==(std::ios_base::fixed|std::ios_base::scientific)), the stream's precision is not used; instead, the number is always printed with enough precision to exactly represent the value. | (since C++11) |
Output a number using the facet directly, and demonstrate user-defined facet:
#include <iostream>#include <locale> // this custom num_put outputs squares of all integers (except long long)struct squaring_num_put:std::num_put<char>{ iter_type do_put(iter_type out,std::ios_base& str, char_type fill,long val)const{returnstd::num_put<char>::do_put(out, str, fill, val* val);} iter_type do_put(iter_type out,std::ios_base& str, char_type fill,unsignedlong val)const{returnstd::num_put<char>::do_put(out, str, fill, val* val);}}; int main(){auto& facet=std::use_facet<std::num_put<char>>(std::locale()); facet.put(std::cout,std::cout,'0',2.71);std::cout<<'\n'; std::cout.imbue(std::locale(std::cout.getloc(), new squaring_num_put));std::cout<<6<<' '<<-12<<'\n';}
Output:
2.7136 144
An implementation ofoperator<< for a user-defined type.
#include <iostream>#include <iterator>#include <locale> struct base{long x=10;}; template<class CharT,class Traits>std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,const base& b){try{typenamestd::basic_ostream<CharT, Traits>::sentry s(os); if(s){std::ostreambuf_iterator<CharT, Traits> it(os);std::use_facet<std::num_put<CharT>>(os.getloc()) .put(it, os, os.fill(), b.x);}}catch(...){// set badbit on os and rethrow if required} return os;} int main(){ base b;std::cout<< b;}
Output:
10
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 34 | C++98 | thebool overload used non-existing members truename andfalsename ofstd::ctype | use these members ofstd::numpunct |
| LWG 231 | C++98 | the precision modifier was only added if (flags& fixed)!=0 orstr.precision()>0 | removed these conditions |
| LWG 282 | C++98 | the thousand separators were only inserted for integral types in stage 2 | also inserted for floating-point types |
| LWG 4084 | C++11 | "NAN" and"INF" could not be printed | they can be printed |
| inserts formatted data (public member function of std::basic_ostream<CharT,Traits>)[edit] |