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

How to use setfill in c++

下次还敢
下次还敢Original
2024-05-01 12:00:28792browse

setfill is a member function of the C stream operator that sets the fill character of the unfilled characters in the stream to fill the unfilled fields produced by the insertion operator. Syntax: ostream& setfill(char ch), where ch is the character to be set as the fill character. Example: setfill('') can fill a 10-bit wide integer or string with asterisks, such as 123 or *Hello.

How to use setfill in c++

Use of setfill in C

What is setfill?

setfill is a member function of the stream operator in C, used to set the fill character of unfilled characters in the stream. These characters are used to populate unpopulated fields in the stream, typically resulting from the insertion operator (<<).

How to use setfill

To use the setfill function, you need to write the following syntax:

<code class="cpp">ostream& setfill(char ch);</code>

where ch is the character to be set as the fill character.

Example

The following example demonstrates how to use the setfill function:

<code class="cpp">#include <iostream>

using namespace std;

int main() {
    // 设置填充字符为星号 (*)
    cout.setfill('*');

    // 输出一个 10 位宽的整数
    cout << setw(10) << 123 << endl;

    // 输出一个 10 位宽的字符串
    cout << setw(10) << "Hello" << endl;

    return 0;
}</p>
<p>Output: </p>
<pre class="brush:php;toolbar:false"><code>********123
******Hello</code>

As you can see, the numbers and The strings are all padded in front with asterisks, and the padding width is 10.

Note

  • The setfill function will only affect subsequent output operations.
  • The setfill function defaults to fill characters with spaces.
  • You can use any character (including spaces) as a padding character.

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