为什么 std::vector<bool>没有.data()? [英] Why does std::vector<bool> have no .data()?

查看:22
本文介绍了为什么 std::vector<bool>没有.data()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::vector 的特化,在 C++11 23.3.7/1 中指定,不声明 data() 成员(例如提到

假设你想索引一个块来获取一个值,你会如何使用操作符[]?它不能返回 bool& (因为它会返回一个字节,其中存储了多个 bools),因此你不能分配一个 bool* 给它.换句话说,bool *bool_ptr =&v[0];not 有效代码,会导致编译错误.

此外,正确的实现可能没有这种专门化并且不进行内存优化(压缩).所以 data() 必须根据实现复制到预期的返回类型(或者标准应该强制优化而不是仅仅允许它).

<块引用>

为什么不能返回指向布尔数组的指针?

因为 std::vector 存储为 bool 数组,因此无法直接返回指针.它可以通过将数据复制到一个数组并返回该数组来做到这一点,但不这样做是一种设计选择(如果他们这样做了,我认为这对于所有的人来说都是 data()容器,这会产生误导).

<块引用>

不这样做有什么好处?

内存优化.

通常会减少 8 倍的内存使用量,因为它将多个位存储在一个字节中.确切地说,CHAR_BIT 次.

The specialisation of std::vector<bool>, as specified in C++11 23.3.7/1, doesn't declare a data() member (e.g. mentioned here and here).

The question is: Why does a std::vector<bool> have no .data()? This is the very same question as why is a vector of bools not stored contiguously in memory. What are the benefits in not doing so?

Why can a pointer to an array of bools not be returned?

解决方案

Why does a std::vector have no .data()?

Because std::vector<bool> stores multiple values in 1 byte.

Think about it like a compressed storage system, where every boolean value needs 1 bit. So, instead of having one element per memory block (one element per array cell), the memory layout may look like this:

Assuming that you want to index a block to get a value, how would you use operator []? It can't return bool& (since it will return one byte, which stores more than one bools), thus you couldn't assign a bool* to it. In other words bool *bool_ptr =&v[0]; is not valid code, and would result in a compilation error.

Moreover, a correct implementation might not have that specialization and don't do the memory optimization (compression). So data() would have to copy to the expected return type depending of implementation (or standard should force optimization instead of just allowing it).

Why can a pointer to an array of bools not be returned?

Because std::vector<bool> is not stored as an array of bools, thus no pointer can be returned in a straightforward way. It could do that by copying the data to an array and return that array, but it's a design choice not to do that (if they did, I would think that this works as the data() for all containers, which would be misleading).

What are the benefits in not doing so?

Memory optimization.

Usually 8 times less memory usage, since it stores multiple bits in a single byte. To be exact, CHAR_BIT times less.

这篇关于为什么 std::vector&lt;bool&gt;没有.data()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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