formatparams.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 30 行

CPP
30
字号
#include <iostream>
#include <iomanip>
using namespace std;

void output(ostream& out, ios_base::fmtflags flags)
// post: print using flags, restore old flags
{
    ios_base::fmtflags oldflags = out.flags(flags);

    out << "oldflags: " << oldflags << "\tnew: "
        << out.flags() << "\t";

    out << 12.47 << "\t" << true << "\t" 
        << 99.0  << "\t" << 255 << endl;

    out.flags(oldflags);  // restore as before
}

int main()
{
    output(cout, cout.flags());        // default
    output(cout, ios_base::showpos);   // leading +
    output(cout, ios_base::boolalpha); // print true
    output(cout, ios_base::showpoint); // show .0

    output(cout, ios_base::boolalpha|ios_base::hex);  // bool on, base 16
    output(cout, ios_base::hex|ios_base::showbase);   // show 0x in front
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?