std::setw(3)
std::setw(3) π
β¨
okay bestie, welcome to the C++ Standard Library manpage corner of the universe β this one is std::setw(3), and it's literally about setting stream width. no cap, it's niche but iconic. π
πΈ NAME
std::setw β std::setw
π Synopsis
Defined in header <iomanip>
/* unspecified */ setw( int n );
When used in an expression out << std::setw(n) or in >> std::setw(n), sets the width parameter of the stream out or in to exactly n.
Some operations reset the width to zero (see below), so std::setw may need to be repeatedly called to set the width for multiple operations.
ποΈ Parameters
nβ new value for width
π Return value
An object of unspecified type such that:
- if
outis an object of typestd::basic_ostream<CharT, Traits>, the expressionout << setw(n)- has type
std::basic_ostream<CharT, Traits>& - has value
out - behaves as if it called
f(out, n)
- has type
- if
inis an object of typestd::basic_istream<CharT, Traits>, the expressionin >> setw(n)- has type
std::basic_istream<CharT, Traits>& - has value
in - behaves as if it called
f(in, n)
- has type
where the function f is defined as:
void f(std::ios_base& str, int n)
{
// set width
str.width(n);
}
π Notes
The width property of the stream will be reset to zero (meaning "unspecified") if any of the following functions are called:
- Input
operator>>(basic_istream&, basic_string&)operator>>(basic_istream&, char*)
- Output
- Overloads of
basic_ostream::operator<<()that take arithmetic types orvoidpointers (at Stage 3 ofnum_put::put()) operator<<(basic_ostream&, char)andoperator<<(basic_ostream&, char*)operator<<(basic_ostream&, basic_string&)std::put_money(insidemoney_put::put())std::quoted(when used with an output stream)
- Overloads of
The exact effects this modifier has on the input and output vary between the individual I/O functions and are described at each operator<< and operator>> overload page individually.
π» Example
Run this code:
#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"
π©Ή Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 183Β· applied to C++98 β behavior as published:setwcould only be used with streams of typestd::ostreamorstd::istream; correct behavior: usable with any character stream.
π See also
widthβ manages field width (public member function ofstd::ios_base)setfillβ changes the fill character (function template)internalβ sets the placement of fill charactersleftβ (function)rightβ (function)showbaseβ controls whether prefix is used to indicate numeric basenoshowbaseβ (function)
π source/version vibes: http://cppreference.com, 2024.06.10, page std::setw(3) β bye bestie, go format something cute! πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms