本文介绍了矢量和布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)

是C ++足够智能自动使用位对于bool或将

a bool具有字符(字节)的大小。

2)向量的索引是整数(4字节)或长的

有8个字节还是其他东西?


我问因为我想用


vector< ; BOOL>有几十亿个条目超过了int32范围的

索引,我希望尽可能少地使用整个

向量< bool>


例如

vector< bool> B;

B.resize(2 ^ 34);


谢谢,daniel

解决方案



我们可以询问什么类型的应用程序或什么样的数据?

在许多情况下,使用某种形式的压缩可能更好

分配千兆字节的RAM。你的系统有足够的内存吗?

问候,

伊万

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格





我肯定有''这里的人以前做过这件事。 。 。


我不是其中之一!

-JKop





AFAIK,有些系统甚至使用4个字节进行布尔,因为他们可以更快地访问它。

这与系统的实际CPU有关,
内存的最小单位是多少可以访问,其被称为字节,其中10次的9次等于8位。


但是,CPU字节可能与C ++字节不同。我曾在那里听到过

是信号处理器,它具有24位的最小内存单位,而且仍然是b ++实现为你提供了8位的字符。并且有4位

CPUS需要组合两个字节以满足最低要求

为8位的char。我想可能有C实现,但可能是这些CPU上没有C ++的C实现,但在这种情况下C具有相同的要求。

将存储8个bool的代码1个字节会更慢并且(具有讽刺意味)更多的内存消耗,因为操作字节的代码将被保存在内存中。




那是对的。

例如:

如果是char是9比特,然后虽然长至少是32位,它不一定是4个字节长,因为该系统上4个字节= 36位。




那个例子并不是真的很好。 3个字节只有27位,

所以你仍然需要4个字节才能满足至少32个b $ b位的要求。


1)
is C++ smart enough to automatically use "bits" for bool or will
a bool have the size of a charcter (byte).
2) The index of a vector is it an integer (4 byte) or a "long long"
with 8 bytes or something else?

I ask because I want to use

vector<bool> with a few billion entries exceeding the int32 range for
indexing and I want to use as few memory as possible for the whole
vector<bool>

e.g.
vector<bool> B;
B.resize(2^34);

thanks, daniel

解决方案


May we ask for what type of application or what kind of data?
In many cases, using some form of compression might be better
that allocating gigabytes of RAM. Does your system have enough
memory?
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form




I''m sure there''s people here that''ve done this before. . .

I''m not one of them!
-JKop




AFAIK, some systems use even 4 bytes for a bool, because they can access it
faster.
It''s to do with the actual CPU of the system, what is the smallest unit of
memory that can be accessed, which is called a "byte", which 9 times out
of 10 is equal to "8 bits".
However, a CPU byte might be different from a C++ byte. I have heared there
are signal processors that have a smallest memory unit of 24 bits and still
the C++ implementation gives you a char with 8 bit. And there are 4 bit
CPUS which would need to combine two bytes to meet the minimum requirement
of 8 bits for char. I guess there might be C implelemntations, but probably
no C++ ones on such CPUs, but C has the same requirements in that case.
Code which would store 8 bools in 1 byte would be slower and (ironically)
more memory consumptive as the code which manipulates the byte would have
to be kept in memory.



That''s right.
For instance:

If "char" were 9-Bit, then although "long" would be atleast 32-Bit, it
wouldn''t necessarily be 4 bytes long, as 4 bytes = 36 bits on that system.



That example isn''t really chosen very well. 3 bytes would only be 27 bits,
so you would still need 4 bytes to meet the requirement of at least 32
bits.


这篇关于矢量和布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:32