| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Floating-point formatting | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
| Integer formatting | |||||||||||||||||||||||||||||||
| Boolean formatting | |||||||||||||||||||||||||||||||
| Field width and fill control | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
| Other formatting | |||||||||||||||||||||||||||||||
| Whitespace processing | |||||||||||||||||||||||||||||||
| Output flushing | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
| Status flags manipulation | |||||||||||||||||||||||||||||||
| Time and money I/O | |||||||||||||||||||||||||||||||
| Quoted manipulator | |||||||||||||||||||||||||||||||
(C++14) | |||||||||||||||||||||||||||||||
Defined in header <iomanip> | ||
/* unspecified */ setw(int n); | ||
When used in an expressionout<< std::setw(n) orin>> std::setw(n), sets thewidth parameter of the streamout orin to exactlyn.
Some operations reset the width to zero (seebelow), sostd::setw may need to be repeatedly called to set the width for multiple operations.
Contents |
| n | - | new value for width |
An object of unspecified type such that
where the functionf is defined as:
void f(std::ios_base& str,int n){// set width str.width(n);}
The width property of the stream will be reset to zero (meaning "unspecified") if any of the following functions are called:
The exact effects this modifier has on the input and output vary between the individual I/O functions and are described at eachoperator<< andoperator>> overload page individually.
#include <iomanip>#include <iostream>#include <sstream> int main(){std::cout<<"no setw: ["<<42<<"]\n"<<"setw(6): ["<< std::setw(6)<<42<<"]\n"<<"no setw, several elements: ["<<89<<12<<34<<"]\n"<<"setw(6), several elements: ["<<89<< std::setw(6)<<12<<34<<"]\n"; std::istringstream is("hello, world");char arr[10]; is>> std::setw(6)>> arr;std::cout<<"Input from\""<< is.str()<<"\" with setw(6) gave\""<< arr<<"\"\n";}
Output:
no setw: [42]setw(6): [ 42]no setw, several elements: [891234]setw(6), several elements: [89 1234]Input from "hello, world" with setw(6) gave "hello"
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 183 | C++98 | setw could only be used with streams oftypestd::ostream orstd::istream | usable with any character stream |
| manages field width (public member function of std::ios_base)[edit] | |
| changes the fill character (function template)[edit] | |
| sets the placement of fill characters (function)[edit] | |
| controls whether prefix is used to indicate numeric base (function)[edit] |