位集参考 [英] Bitset Reference

查看:25
本文介绍了位集参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 http://www.cplusplus.com/reference/stl/bitset/:

因为在大多数 C++ 环境中不存在这样的小元素类型,所以单个元素作为模拟 bool 元素的特殊引用被访问.

Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements.

这个位参考究竟是如何工作的?

How, exactly, does this bit reference work?

我能想到的唯一方法是使用 char 的静态数组,但是每个实例都需要将其索引存储在数组中.由于每个引用实例至少具有 size_t 的大小,这会破坏位集的紧凑性.此外,调整大小可能很慢,而位操作预计会很快.

The only way I could think of would be to use a static array of chars, but then each instance would need to store its index in the array. Since each reference instance would have at least the size of a size_t, that would destroy the compactness of the bitset. Additionally, resizing may be slow, and bit manipulation is expected to be fast.

推荐答案

我认为你混淆了两件事.

I think you are confusing two things.

bitset 类以紧凑的表示形式存储位,例如在 char 数组中,通常每个 char 8 位(但在异国情调"平台上是 YMMV).

The bitset class stores the bits in a compact representations, e.g. in a char array, typically 8 bits per char (but YMMV on "exotic" platforms).

提供bitset::reference 类是为了允许bitset 类的用户对存储在bitset 中的位具有类似引用的对象代码>.

The bitset::reference class is provided to allow users of the bitset class to have reference-like objects to the bits stored in a bitset.

因为常规指针和引用没有足够的粒度来指向存储在 bitset 中的单个位(它们的最小粒度是 char),这样的类模仿对位上的伪引用类左值操作的引用的语义.这尤其需要允许 operator[] 返回的值作为左值正常"工作(并且它可能构成其正常"使用的 99%).在这种情况下,它可以被视为一个代理对象".

Because regular pointers and references don't have enough granularity to point to the single bits stored in the bitset (their minimum granularity is the char), such class mimics the semantic of a reference to fake reference-like lvalue operations on the bits. This is needed, in particular, to allow the value returned by operator[] to work "normally" as an lvalue (and it probably costitutes 99% of its "normal" use). In this case it can be seen as a "proxy-object".

此行为是通过重载赋值运算符和转换为bool 运算符来实现的;bitset::reference 类可能会封装对父 bitset 对象的引用和被引用位的偏移量(字节+位),这些运算符用于检索并存储该位的值.

This behavior is achieved by overloading the assignment operator and the conversion-to-bool operator; the bitset::reference class will probably encapsulate a reference to the parent bitset object and the offset (bytes+bit) of the referenced bit, that are used by such operators to retrieve and store the value of the bit.

---编辑---

实际上,g++ 实现使 bitset::reference 直接存储指向存储字节的内存字的指针,以及该字中的位数.然而,这只是提高其性能的实现细节.

Actually, the g++ implementation makes the bitset::reference store directly a pointer to the memory word in which the byte is stored, and the bit number in such word. This however is just an implementation detail to boost its performance.

顺便说一下,在库源中,我发现了一个非常简洁但清晰的解释 bitset::reference 是什么以及它的作用:

By the way, in the library sources I found a very compact but clear explanation of what bitset::reference is and what it does:

  /**
   *  This encapsulates the concept of a single bit.  An instance of this
   *  class is a proxy for an actual bit; this way the individual bit
   *  operations are done as faster word-size bitwise instructions.
   *
   *  Most users will never need to use this class directly; conversions
   *  to and from bool are automatic and should be transparent.  Overloaded
   *  operators help to preserve the illusion.
   *
   *  (On a typical system, this <em>bit %reference</em> is 64
   *  times the size of an actual bit.  Ha.)
   */

这篇关于位集参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆