std::ios_base::setf
来自cppreference.com
| fmtflags setf( fmtflags flags ); |
(1) | |
| fmtflags setf( fmtflags flags, fmtflags mask ); |
(2) | |
设置格式化标志以指定设置。
1) 设置
flags 所标识的格式化标志。等效地进行下列操作: fl = fl | flags ,其中 fl 定义内部格式化标志的状态。2) 清除
mask 下的格式化标志,并设置被清除的标志为 flags 所指定者。等效地进行下列操作: fl = (fl & ~mask) | (flags & mask) ,其中 fl 定义格式化标志的内部状态。参数
| flags, mask | - | 新格式化设定。 mask 定义哪些标志可以改变, flags 定义要改变的标志中该设置那些(其他将被清除)。两个参数都能为下列常量的组合:
| ||||||||||||||||||||||||||||||||||||||||
返回值
调用函数前的格式化标志
示例
运行此代码
#include <iostream> #include <iomanip> const double PI = 3.1415926535; int main() { const int WIDTH = 15; std::cout.setf(std::ios::right); // 等价: cout << right; std::cout << std::setw(WIDTH/2) << "radius" << std::setw(WIDTH) << "circumference" << '\n'; std::cout.setf(std::ios::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'; } }
输出:
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参阅
| 管理格式标志 (公开成员函数) | |
| 清除特定格式的标志 (公开成员函数) |