【问题标题】:set basefield to zero将基域设置为零
【发布时间】:2013-08-29 19:39:42
【问题描述】:

我知道basefielddec/oct/hex 的掩码,我只想使用setf 流函数取消设置它们但无论我尝试多少都不正确,所以我编写了以下示例来帮助我了解 flags 如何在内部查找 basefield 的所有可能值。

#include <iostream>
using namespace std;

int main()
{
    typedef ios_base::fmtflags fmt;
    fmt a, b, c = cout.basefield;

    cout << "basefield: " << hex << c << endl;

    cout << dec;
    a = cout.flags();
    b = a & c;
    cout << "\ndec flags:\n";
    cout << "   a: " << hex << a << endl;
    cout << "   b: " << hex << b << endl;

    cout << oct;
    a = cout.flags();
    b = a & c;
    cout << "\noct flags:\n";
    cout << "   a: " << hex << a << endl;
    cout << "   b: " << hex << b << endl;

    cout << hex;
    a = cout.flags();
    b = a & c;
    cout << "\nhex flags:\n";
    cout << "   a: " << hex << a << endl;
    cout << "   b: " << hex << b << endl;

    cout.setf(cout.flags() & ~cout.basefield);
    a = cout.flags();
    b = a & c;
    cout << "\nunset basefiled:\n";
    cout << "   a: " << hex << a << endl;
    cout << "   b: " << hex << b << endl;
}

g++ 4.8.1 结果是:

basefield: 4a

dec flags:
   a: 1002
   b: 2

oct flags:
   a: 1040
   b: 40

hex flags:
   a: 1008
   b: 8

unset basefiled:
   a: 1008
   b: 8

当未设置所有基标志值设置为与十六进制标志相同时,我运行代码 Visual C++ 2008 和 Intel C++ XE,结果是:

basefield: e00

dec flags:
   a: 201
   b: 200

oct flags:
   a: 401
   b: 400

hex flags:
   a: 801
   b: 800

unset basefiled:
   a: 801
   b: 800

十六进制标志似乎与所有未设置的基数完全相同。

我想要的只是将标志设置为dec\oct\hex 以外的值,这样当我实现自己的操纵器时,我可以检测我的格式或流格式正在使用哪种格式。

谢谢。

【问题讨论】:

  • 这不是创建自己的操纵器的方法
  • 我想创建bin 机械手,有没有更好的方法?
  • 不,这是不可能的。您需要创建一个新的操纵器。也许你应该问一个新问题。

标签: c++ stl iomanip


【解决方案1】:

您不能使用单个参数取消设置 setf 重载的标志 - 它只能设置新标志。使用带有两个参数的unsetfsetf 来取消设置标志。

【讨论】:

    猜你喜欢
    • 2014-10-19
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2011-10-12
    • 2018-08-23
    • 1970-01-01
    相关资源
    最近更新 更多