Home  >  Article  >  Backend Development  >  How to use setfill in c++

How to use setfill in c++

下次还敢
下次还敢Original
2024-05-01 12:03:16473browse

The setfill() function is used in C to set the fill character of the output stream. Its usage is: std::setfill(char ch); This function fills the output value to the specified width, such as: • std: :cout << std::setfill(' ') << std::setw(10) << 3.14 << std::endl; will output: 3.14

How to use setfill in c++

setfill() function usage in C

setfill() function is a function provided by the header file in C. Used to set the padding character for the output stream. Its usage is as follows:

<code class="cpp">std::setfill(char ch);</code>

where ch is the padding character to be set.

Detailed Usage

The setfill() function is used to output specific characters in the output stream until the specified width is reached. This width is usually set by the setw() function. If the output value is smaller than the specified width, it is padded with the specified characters.

For example, the following code will output the decimal 3.14, padded with spaces to a width of 10:

<code class="cpp">std::cout << std::setfill(' ') << std::setw(10) << 3.14 << std::endl;</code>

This will output:

<code>     3.14</code>

Notes

  • The setfill() function does not affect the width of the output stream, it only sets the fill characters.
  • Padding characters can only be single-byte characters.
  • The setfill() function does not modify the flags of the output stream, such as showpos or left.

The above is the detailed content of How to use setfill in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn