std::num_put::put, std::num_put::do_put

From Cppreference

Jump to: navigation, search
Defined in header <locale>

public:

iter_type put( iter_type out, std::ios_base& str, char_type fill, bool v) const;
iter_type put( iter_type out, std::ios_base& str, char_type fill, long v) const;
iter_type put( iter_type out, std::ios_base& str, char_type fill, long long v) const;
iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long v) const;
iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long long v) const;
iter_type put( iter_type out, std::ios_base& str, char_type fill, double v) const;
iter_type put( iter_type out, std::ios_base& str, char_type fill, long double v) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, const void* v) const;
(1)
protected:

iter_type do_put( iter_type out, std::ios_base& str, char_type fill, bool v) const;
iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long v) const;
iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long long v) const;
iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long) const;
iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long long) const;
iter_type do_put( iter_type out, std::ios_base& str, char_type fill, double v) const;
iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long double v) const;

iter_type do_put( iter_type out, std::ios_base& str, char_type fill, const void* v) const;
(2)

1) public member function, calls the protected virtual member function do_put of the most derived class.

2) writes characters to the output sequence out which represent the value of v, formatted as requested by the formatting flags str.flags() and the std::numpunct and std::ctype facets of the locale imbued in the stream str. This function is called by all formatted output stream operators, such as std::cout << n;.

Conversion occurs in four stages

Contents

[edit] Stage 1: conversion specifier selection

fmtflags basefield = (str.flags() & std::ios_base::basefield);
fmtflags uppercase = (str.flags() & std::ios_base::uppercase);
fmtflags floatfield = (str.flags() & std::ios_base::floatfield);
fmtflags showpos = (str.flags() & std::ios_base::showpos);
fmtflags showbase = (str.flags() & std::ios_base::showbase);
If basefield == oct, will use conversion specifier %o
If basefield == hex && !uppercase, will use conversion specifier %x
If basefield == hex, will use conversion specifier %X
If the type of v is signed, will use conversion specifier %d
If the type of v is unsigned, will use conversion specifier %u
If floatfield == std::ios_base::fixed, will use conversion specifier %f
If floatfield == std::ios_base::scientific && !uppercase, will use conversion specifier %e
If floatfield == std::ios_base::scientific, will use conversion specifier %E
If floatfield == (std::ios_base::fixed | std::ios_base::scientific) && !uppercase, will use conversion specifier %a (since C++11)
If floatfield == std::ios_base::fixed | std::ios_base::scientific, will use conversion specifier %A (since C++11)
If !uppercase, will use conversion specifier %g
otherwise, will use conversion specifier %G

[edit] Stage 2: locale-specific conversion

[edit] Stage 3: padding

If adjustfield == std::ios_base::left, will pad after
If adjustfield == std::ios_base::right, will pad before
If adjustfield == internal and a sign character occurs in the representation, will pad after the sign
If adjustfield == internal and Stage 1 representation began with 0x or 0X, will pad after the x or X
otherwise, will pad before

[edit] Stage 4: output

Every successive character c from the sequence of CharT's from Stage 3 is output as if by *out++ = c.

[edit] Parameters

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
v - value to convert to string and output

[edit] Return value

out

[edit] Notes

The leading zero generated by the conversion specification #o (resulting from the combination of std::showbase and std::oct for example) is not counted as a padding character.

[edit] Example

Output a number using global locale

#include <iostream>
#include <locale>
 
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';
}

Output:

2,71

[edit] See also

inserts formatted data
(public member function of std::basic_ostream)