Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the setw () function of C++

Shulou Source: shulou.com Published: 2022-06-01 17:28:59 11月16日 Update

This article mainly explains the "C++ setw () function how to use", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "C++ setw () function how to use it!"

Setw (int n) is the field width setting used in the output operation in C++. It sets the field width of the output, where n represents the field width. It is only valid for the following output, and then changes back to the default domain width at the end of the subsequent output. When the length of the following output field is less than n, fill in front of the field with a space; when the length of the output field is greater than n, all are output as a whole.

The syntax format of C++ setw () is as follows:

Setw (n)

N stands for width and is represented by numbers.

The setw () function only works on the output that follows.

When the length of the following output field is less than n, fill it with a space in front of the field, and when the length of the output field is greater than n, the whole output is output.

The following example demonstrates the use of the setw () function:

Example

# include # include using namespace std;int main () {/ / begins with a width of 4, and the subsequent runoob character length is greater than 4, so it does not work: cout "runoob", "runoob" return 0;}

The output of the above code is:

Runoobrunoobrunoob runoobrunoobrunoob

Setw () is filled with spaces by default, and can be padded with setfill () to set other characters.

Example # include # include using namespace std;int main () {cout'*') "runoob" return 0;}

The output of the above code is:

* runoob Thank you for your reading. The above is the content of "how to use the setw () function of C++". After the study of this article, I believe you have a deeper understanding of how to use the setw () function of C++. The specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Output field function clocked length width content instance space followed by learning code function character whole time result valid that is the beginning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Apple Microsoft Shulou Tech Info