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.
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 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!