【问题标题】:'unsigned variable: value' C++ [duplicate]'无符号变量:值' C++ [重复]
【发布时间】:2014-08-12 07:26:06
【问题描述】:

我正在处理三元搜索树,我在三元搜索树节点结构中遇到了不同的事情:

struct Node {
    char data;

    // True if this character is last character of one of the words
    unsigned isEndOfString: 1;

    struct Node *left, *eq, *right;
};

它与 C++11 和旧版本均兼容。我想知道这个unsigned isEndOfString: 1 是什么意思?它与 bool isEndOfString = true 有何不同?该语句实际指的是什么类型,何时使用这种语法方便?

【问题讨论】:

标签: c++ c++11 boolean unsigned


【解决方案1】:

它是大小为 1 的 bit field。所以 isEndOfString 是一个值,其值为 0 或 1。

基本上,它允许您在此处将布尔标志存储在一个位中,从而最大限度地减少 Node 的内存占用。

注意位域是从 C 继承的。

【讨论】:

  • 谢谢!你能告诉我unsigned默认是unsigned char还是unsigned int
  • unsigned 在标准 C++ 中默认为 unsigned int
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2017-03-05
  • 2016-09-27
  • 2014-12-07
  • 1970-01-01
  • 2020-11-21
相关资源
最近更新 更多