矢量和布尔 [英] vector and bool

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

问题描述

1)

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

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

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

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


我问因为我想用


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

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

向量< bool>


例如

vector< bool> B;

B.resize(2 ^ 34);


谢谢,daniel

解决方案
"丹尼尔" <哒************ @ netscape.net>在消息中写道

news:b3 ************************** @ posting.google.c om ...

1)
是C ++足够智能自动使用位对于bool或将布尔有一个charcter(字节)的大小。
标准C ++库实际上需要专门为bool设计

std :: vector,并提供紧凑存储。

[注意:对于许多用途,这实际上被视为一种不便]

2)向量的索引是一个整数(4字节)或一个long long
8字节或别的什么?
这取决于实现,但可能会受到限制

到size_t,或许多平台上的4字节无符号整数。

你可以查看向量的返回值< bool> :: max_size()到

找出来。

我问因为我想使用

vector< bool> ;有几十亿条目超过了int32范围的
索引,我想尽可能少地使用整个
向量< bool>



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

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

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

问候,

伊万

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


daniel发布:

1)
是C ++智能足以自动使用"位"对于bool或将布尔有一个charcter(字节)的大小。


我认为sizeof(bool)必须是> = 1,这意味着bool是一个字节的
。但这并不意味着你的特定系统可能会在背景中做出不同的事情。


这与实际的CPU有关系统,可以访问的内存的最小单位是什么,这被称为字节,其中9次超出

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

2)向量的索引是一个整数(4个字节)还是一个long long
,有8个字节或其他东西?


没有这样的内在类型long long。


有一个类型long int,保证在所有

系统上至少32位,但这并不能保证它会长4个字节。对于

实例:


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

我问因为我想要使用

vector< bool>有几十亿条目超过int32范围进行索引,我想尽可能少地使用整个
向量< bool>

向量< ; BOOL> B;
B.resize(2 ^ 34);

谢谢,daniel




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


我不是其中之一!

-JKop


JKop写道:

daniel发布:

1)
是C ++智能足以自动使用位对于bool或者将bool具有字符大小(字节)。
我认为sizeof(bool)必须是> = 1,这意味着bool。是一个字节的
。但这并不意味着你的特定系统可能会在背景中做出不同的事情。




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

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


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

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

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

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

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

2)向量的索引是一个整数(4个字节)还是一个长长的,有8个字节或其他东西?
没有这样的内在类型long long。

有一个类型long int,在所有系统上都保证至少是32位,但这并不能保证它会长4个字节。




那是对的。

例如:

如果是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

解决方案

"daniel" <da************@netscape.net> wrote in message
news:b3**************************@posting.google.c om...

1)
is C++ smart enough to automatically use "bits" for bool or will
a bool have the size of a charcter (byte). The standard C++ library is actually required to specialize the
std::vector for bool, and to provide ''compact'' storage.
[NB: for many uses, this is actually seen as an inconvenience]
2) The index of a vector is it an integer (4 byte) or a "long long"
with 8 bytes or something else? This depends on the implementation, but it may well be limited
to size_t, or a 4-byte unsigned integer on many platforms.
You can check the return value of vector<bool>::max_size() to
find out.
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>


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


daniel posted:

1)
is C++ smart enough to automatically use "bits" for bool or will
a bool have the size of a charcter (byte).
I believe that sizeof(bool) must be >= 1, which implies that a "bool" is of
one byte. But that doesn''t mean that your particular system might do
something different in the background.

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". 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.
2) The index of a vector is it an integer (4 byte) or a "long long"
with 8 bytes or something else?
There is no such intrinsic type "long long".

There is a type "long int", which is guaranteed to be atleast 32-bit on all
systems, but that doesn''t guarantee that it''ll be 4 bytes long. 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.
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



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

I''m not one of them!
-JKop


JKop wrote:

daniel posted:

1)
is C++ smart enough to automatically use "bits" for bool or will
a bool have the size of a charcter (byte).
I believe that sizeof(bool) must be >= 1, which implies that a "bool" is
of one byte. But that doesn''t mean that your particular system might do
something different in the background.



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.

2) The index of a vector is it an integer (4 byte) or a "long long"
with 8 bytes or something else?
There is no such intrinsic type "long long".

There is a type "long int", which is guaranteed to be atleast 32-bit on
all systems, but that doesn''t guarantee that it''ll be 4 bytes long.



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.


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

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