C++ setw() Function
Overview
In C++, the setw function is a manipulator for set width. The setw method is found in the iomaip library included in C++.
Using the setw function (manipulator), you can specify the width of a field in the iOS library or the minimum number of characters a variable will consume. In simple terms, you can set the width of a field for output operations using the setw C++ function.
Syntax of C++ setw()
Parameters of C++ setw()
C++ setw() takes only one parameter, which is an integer. The field width is determined by the number of characters specified in the argument.
Return Value of C++ setw()
C++ setw() returns nothing and is only used as a stream manipulator.
Exceptions of C++ setw()
Basic guarantee − if an exception is thrown, the stream is valid.
Examples
When the Set Width Is Greater than The Length of The Output
Code:
Output
Explanation:
The code mentioned above explains how the setw function operates. The iomanip library has been imported. The value for the setw() function has then been set to 6 after that. It starts over at zero. Then, as an output, we send a number. You can see the difference in spacing by looking at the result, which is displayed.
When the Set Width Is Equal to The Length of The Output
Code:
Output
Explanation:
The code mentioned above explains how the setw function operates when the set width is equal to the length of the output. The iomanip library has been imported. The value for the setw() function has then been set to 7 after that. It starts over at zero. Then, as an output, we send the number 5676891. Since the set width is equal to the length of the output, the output covers all the widths mentioned.
When the Set Width Is Less than The Length of The Output
Code:
Output
Explanation:
The code mentioned above explains how the setw function operates when the set width is less than the length of the output. The iomanip library has been imported. The value for the setw() function has then been set to 3, less than the length of output 456746.
The size of the number exceeds the size of the output. The setw C++ function's unique feature is that it leaves the number unchanged even if the field's width is less. Instead, it displays the complete number. Therefore, the complete number is returned if the argument supplied is less than the size of the number.
How Does C++ setw() Work?
The setw() function is used to set the width of a field using the number provided as arguments. We can better understand this by looking at the code below.
Code:
Output
Explanation:
We can determine from the code lines that we used standard libraries. There is one with an input-output stream and another with an input-output manipulation library, to which setw() belongs. Without importing these libraries, the function will not function.
The width of the output field is then set by the main function using the setw() function. We output the string as "abc" after setting the setw() function to 8. This field will initially be set to 0, and when the appropriate number is entered, it will be changed to 8 in this case. The following is how it will operate.
As the string is three characters long, the first five spaces will be empty (the above output contains underscores to represent the white spaces). The string we provided as output is used to write the last three positions.
Conclusion
- The setw() function is used to set the width of a field taken as an argument.
- The setw() function takes only one parameter, which is an integer.
- The given argument to the setw() function can be greater than, equal to, or less than the length of the output.
- In each case, the complete data is returned even if the argument supplied is less than the size of the number.
- When the set width exceeds the output's length, the setw() function will leave the first required spaces empty.