Home > Backend Development > C++ > body text

How to use setfill in c++

下次还敢
Release: 2024-05-01 12:03:16
Original
475 people have browsed it

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>
Copy after login

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>
Copy after login

This will output:

<code>     3.14</code>
Copy after login

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!

Related labels:
c++
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template