std::ios_base::setf(3)
π
std::ios_base::setf(3) π
hey bestie, welcome to the C++ Standard Libary manual β yep, Libary, thatβs the spelling, and weβre just gonna roll with it π. this is the page for std::ios_base::setf, and itβs lowkey iconic because itβs all about formatting flags. no cap.
β¨ NAME β¨
std::ios_base::setf - std::ios_base::setf
πΈ Synopsis πΈ
fmtflags setf( fmtflags flags ); (1)
fmtflags setf( fmtflags flags, fmtflags mask ); (2)
Sets the formatting flags to specified settings. okay so hereβs the tea π«:
- Sets the formatting flags identified by
flags. Effectively, the following operation is performed:fl = fl | flags, wherefldefines the state of internal formatting flags. - Clears the formatting flags under
mask, and sets the cleared flags to those specified byflags. Effectively, the following operation is performed:fl = (fl & ~mask) | (flags & mask), wherefldefines the state of internal formatting flags.
π Parameters π
flags,maskβ new formatting setting.maskdefines which flags can be altered, andflagsdefines which flags of those to be altered should be set (others will be cleared). Both parameters can be a combination of the formatting flags constants.
π Formatting flags π
decβ use decimal base for integer I/O: seestd::dec.octβ use octal base for integer I/O: seestd::oct.hexβ use hexadecimal base for integer I/O: seestd::hex.basefieldβdec | oct | hex. Useful for masking operations.leftβ left adjustment (adds fill characters to the right): seestd::left.rightβ right adjustment (adds fill characters to the left): seestd::right.internalβ internal adjustment (adds fill characters to the internal designated point): seestd::internal.adjustfieldβleft | right | internal. Useful for masking operations.scientificβ generate floating point types using scientific notation, or hex notation if combined with fixed: seestd::scientific.fixedβ generate floating point types using fixed notation, or hex notation if combined with scientific: seestd::fixed.floatfieldβscientific | fixed. Useful for masking operations.boolalphaβ insert and extract bool type in alphanumeric format: seestd::boolalpha.showbaseβ generate a prefix indicating the numeric base for integer output, require the currency indicator in monetary I/O: seestd::showbase.showpointβ generate a decimal-point character unconditionally for floating-point number output: seestd::showpoint.showposβ generate a + character for non-negative numeric output: seestd::showpos.skipwsβ skip leading whitespace before certain input operations: seestd::skipws.unitbufβ flush the output after each output operation: seestd::unitbuf.uppercaseβ replace certain lowercase letters with their uppercase equivalents in certain output operations: seestd::uppercase.
β Return value β
The formatting flags before the call to the function. theyβre handing you the old flags, bestie π«Ά.
π» Example π»
// Run this code
#include <iomanip>
#include <iostream>
#include <numbers>
int main()
{
const double PI = std::numbers::pi;
const int WIDTH = 15;
std::cout.setf(std::ios::right); // equivalent: cout << right;
std::cout << std::setw(WIDTH / 2) << "radius"
<< std::setw(WIDTH) << "circumference" << '\n';
std::cout.setf(std::ios::fixed); // equivalent: cout << fixed;
for (double radius = 1; radius <= 6; radius += 0.5)
std::cout << std::setprecision(1) << std::setw(WIDTH / 2)
<< radius
<< std::setprecision(2) << std::setw(WIDTH)
<< (2 * PI * radius) << '\n';
}
β¨ Output β¨
radius circumference
1.0 6.28
1.5 9.42
2.0 12.57
2.5 15.71
3.0 18.85
3.5 21.99
4.0 25.13
4.5 28.27
5.0 31.42
5.5 34.56
6.0 37.70
π See also π
flagsβ manages format flags (public member function).unsetfβ clears specific format flag (public member function).
thatβs the whole page, periodt π
β from the C++ Standard Libary manual, source http://cppreference.com, dated 2024.06.10 π«Ά
π¦ 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