C++ iomanip Library — setfill Function
The C++ function std::setfill behaves as if member fill were called with c as argument on the stream on which it is inserted as a manipulator (it can be inserted on output streams).
It is used to sets c as the stream’s fill character.
Declaration
Following is the declaration for std::setfill function.
Parameters
c − The new fill character for the stream. char_type is the type of characters used by the stream (i.e., its first class template parameter, charT).
Return Value
It returns unspecified. This function should only be used as a stream manipulator.
Exceptions
Basic guarantee − if an exception is thrown, the stream is in a valid state.
Data races
The stream object on which it inserted is modified. Concurrent access to the same stream object may introduce data races.
Example
In below example explains about setfill function.
Let us compile and run the above program, this will produce the following result −
Name already in use
cpp-docs / docs / standard-library / iomanip-functions.md
- Go to file T
- Go to line L
- Copy path
- Copy permalink
- Open with Desktop
- View raw
- Copy raw contents Copy raw contents
Copy raw contents
Copy raw contents
Extracts a monetary value from a stream using the specified format, and returns the value in a parameter.
amount
The extracted monetary value.
use_intl
If true , use international format. The default value is false .
The manipulator returns an object that, when extracted from the stream str , behaves as a formatted input function that calls the member function get for the locale facet money_get associated with str , using use_intl to indicate international format. If successful, the call stores in amount the extracted monetary value. The manipulator then returns str .
Money must be of type long double or an instantiation of basic_string with the same element and traits parameters as str .
Extracts a time value from a stream using the specified format. Returns the value in a parameter as a time structure.
time_ptr
The time in the form of a time structure.
time_format
The format to use to get the time value.
The manipulator returns an object that, when extracted from the stream str , behaves as a formatted input function that calls the member function get for the locale facet time_get associated with str , using tptr to indicate the time structure and fmt to indicate the beginning of a null-terminated format string. If successful, the call stores in the time structure the values associated with any extracted time fields. The manipulator then returns str .
Inserts a monetary amount using the specified format into a stream.
amount
The monetary amount to insert into the stream.
use_intl
Set to true if manipulator should use international format, false if it shouldn’t.
The manipulator returns an object that, when inserted into the stream str , behaves as a formatted output function that calls the member function put for the locale facet money_put associated with str . If successful, the call inserts amount suitably formatted, using use_intl to indicate international format and str.fill() , as the fill element. The manipulator then returns str .
Money must be of type long double or an instantiation of basic_string with the same element and traits parameters as str .
Writes a time value from a time structure to a stream by using a specified format.
time_ptr
The time value to write to the stream, provided in a time structure.
time_format
The format to write the time value.
The manipulator returns an object that, when inserted into the stream str , behaves as a formatted output function . The output function calls the member function put for the locale facet time_put associated with str . The output function uses time_ptr to indicate the time structure and time_format to indicate the beginning of a null-terminated format string. If successful, the call inserts literal text from the format string and converted values from the time structure. The manipulator then returns str .
(New in C++14) An iostream manipulator that enables convenient round-tripping of strings into and out of streams using the >> and << operators.
str
A std::string , char* , string literal or raw string literal, or a wide version of any of these (for example, std::wstring , wchar_t* ).
delimiter
A user-specified character, or wide character, to use as the delimiter for the beginning and end of the string.
escape
A user-specified character, or wide character, to use as the escape character for escape sequences within the string.
This example shows how to use quoted with the default delimiter and escape character using narrow strings. Wide strings are equally supported.
The following example shows how to provide a custom delimiter or escape character:
Clears the specified flags.
mask
The flags to clear.
The manipulator returns an object that, when extracted from or inserted into the stream str , calls str.setf(ios_base::fmtflags, mask) , and then returns str , see setf and fmtflags .
See setw for an example of using resetiosflags .
Set base for integers.
base
The number base.
The manipulator returns an object that, when extracted from or inserted into the stream str , calls str.setf(mask, ios_base::basefield) , and then returns str , see ios_base::basefield . Here, mask is determined as follows:
If base is 8, then mask is ios_base::oct .
If base is 10, then mask is ios_base::dec .
If base is 16, then mask is ios_base::hex .
If base is any other value, then mask is ios_base::fmtflags(0) `.
See setw for an example of using setbase .
Sets the character that will be used to fill spaces in a right-justified display.
Ch
The character that will be used to fill spaces in a right-justified display.
std::setfill
При использовании в выражении out << setfill(c) устанавливает символ — заполнитель потока out в c .
Parameters
| c | — | новое значение для символа заполнения |
Return value
Возвращает объект неопределенного типа, так что если out является именем выходного потока типа std::basic_ostream<CharT, Traits> , то выражение out << setfill(n) ведет себя так, как если бы был выполнен следующий код:
Notes
Текущий символ заполнения может быть получен с помощью std::ostream::fill .
C++ setfill method example
setfill method is defined in header. This method is used to set a fill character for a stream. In this post, we will learn how to use setfill with an example.
Definition of setfill:
setfill is defined as like below:
It takes only one parameter, c, which is the fill character for the stream. char_type is the type of characters used by the stream.
This method doesn’t return anything.
Example of setfill:
Let’s take a look at the below example program:
In this program, we are using setfill to use $ as the fill character. setw is used to give the width as 5. So, if anything is printed using cout of length less than 5, it will add $ to its left.