Comparison between the use of bool arrays and bitset and vector in C ++ and the Occupied Space

Source: Internet
Author: User
Tags bitset

Recently, the project needs to check whether a large two-dimensional array record has used a data record, so I thought of the way to index memory blocks in the operating system, bitmap can be used to set whether to use and find whether it can be used in O (1) replication. However, in C ++, the storage of Boolean variables is 1 byte (8 bit) storage, resulting in a waste of memory space, because the array may be very large (more than 500 W) So I want to use bitset to implement, however, since the size of the array is not determined during the program running, it needs to be determined dynamically, which leads to many problems. The following is a summary of these problems and solutions:

1. bool Array

C ++ specifies that each bool variable occupies one byte, which makes the memory utilization very low, but the use of the bool array is very simple, the following describes how to use bitset and vector <bool>.

Ii. bitset

Bitset is a class in C ++ STL. For details about how to use it and how to call each function, see the C ++ standard library documentation, there are also simple examples introduced in detail. I will not describe them too much. Below I will explain some of my problems.

First, let's take a look at the size of the content space occupied by bitset.

Bitset <1> BS1;

Bitset <8> bs8;

Bitset <16> bs16;

Bitset <24> bs24;

Bitset <50> bs50;

Bitset <70> bs70;

Bitset <150> bs150;

Bitset <220> bs220;

 

 

Cout <"size of BS1 is \ t" <sizeof (BS1) <Endl;

Cout <"size of bs8 is \ t" <sizeof (bs8) <Endl;

Cout <"size of bs16 is \ t" <sizeof (bs16) <Endl;

Cout <"size of bs24 is \ t" <sizeof (bs24) <Endl;

Cout <"size of bs50 is \ t" <sizeof (bs50) <Endl;

Cout <"size of bs70 is \ t" <sizeof (bs70) <Endl;

Cout <"size of bs150 is \ t" <sizeof (bs150) <Endl;

Cout <"size of bs220 is \ t" <sizeof (bs220) <Endl;

The output in VC is:

Size of BS1 is 4

Size of bs8 is 4

Size of bs16 is 4

Size of bs24 is 4

Size of bs50 is 8

Size of bs70 is 16

Size of bs150 is 24

Size of bs220 is 32

We can see that the space occupied by bitset is indeed much smaller than that of the bool data, but there is still something I don't understand, why should we use four bytes when we only have one digit? If we cannot cross-border, it should also be one or two bytes. So I will refer to the relevant materials, when calculating the size of a bitset, it is not simply a rule that does not allow cross-border computing. The calculation rule is that the size of a 32-bit machine is 4 * (n + 31) /32) on 64-bit machines, the size is 8 * (n + 63)/64), which makes it difficult to get the result. 4*(1 + 31)/32)
= 4. This actually improves the read/write efficiency of the memory. It is indeed a clever practice.

However, bitset must determine the variable size when defining a variable. Although bitset is also a container, it does not dynamically increase the capacity as a vector or other container, and its capacity cannot be determined dynamically, for example, the following code:

Int x = 3;

Bitset <x> B1; // error! The VC reports the following error: intelliisense: expression must have a constant value.

Bitset <10> BS; // right!

Therefore, we can conclude that the bitset must be determined first when used, and bitset does not provide an interface to modify its size and add or delete elements, so it cannot be modified. This limits my use in this program, so I used the vector <bool> method.

3. Vector <bool>

Vector <bool> is actually a pseudo container. It does not save the real bool, but packs bool to save space. In a typical implementation, each "bool" stored in the "vector" occupies a separate bit, and an 8-bit byte will hold 8 "bool ". Internally, vector <bool> uses the Equivalent Concept of bitfield to represent the bool it pretends to hold. This actually saves space. Let's take a look at the test:

Vector <bool> bv1 (1, false );

Vector <bool> bv8 (8, false );

Vector <bool> bv16 (16, false );

Vector <bool> bv24 (24, false );

Vector <bool> bv50 (50, false );

Vector <bool> bv70 (70, false );

Vector <bool> bv150 (150, false );

Vector <bool> bv220 (220, false );

Cout <"size of bv1 is \ t" <bv1.capacity () <Endl;

Cout <"size of bv8 is \ t" <bv8.capacity () <Endl;

Cout <"size of bv16 is \ t" <bv16.capacity () <Endl;

Cout <"size of bv24 is \ t" <bv24.capacity () <Endl;

Cout <"size of bv50 is \ t" <bv50.capacity () <Endl;

Cout <"size of bv70 is \ t" <bv70.capacity () <Endl;

Cout <"size of bv150 is \ t" <bv150.capacity () <Endl;

Cout <"size of bv220 is \ t" <bv21_capacity () <Endl;

The output result is as follows:

Size of bv1 is 32

Size of bv8 is 32

Size of bv16 is 32

Size of bv24 is 32

Size of bv50 is 64

Size of bv70 is 96

Size of bv150 is 160

Size of bv220 is 224

The unit of size here is bit. We can see that the size is the same as the size applied by bitset, and the calculation method is the same. However, an error occurs when I use the following method to read data in the container:

Vector <bool> BV (10, false );

Vector <bool>: iteratorit = BV. Begin ();

Inti = 1;

For (I = 0; I <10; I ++)

{

Cout <"element \ t" <I <"\ tis \ t" <BV [I] <Endl;

I ++;

}

Output:

Element 0 is 0

Element 2 is 0

Element 4 is 0

Element 6 is 0

Element 8 is 0

I found that a part is missing. This should be due to the fact that the internal vector <bool> uses the Equivalent Concept of bitfield to represent the bool it pretends to hold, so I switched to the iterator:

Vector <bool> BV (10, false );

Vector <bool>: iteratorit = BV. Begin ();

Inti = 1;

While (it! = BV. End ())

{

Cout <"element \ t" <I <"\ tis \ t" <* It <Endl;

It ++;

I ++;

}

Output result:

Element 1 is 0

Element 2 is 0

Element 3 is 0

Element 4 is 0

Element 5 is 0

Element 6 is 0

Element 7 is 0

Element 8 is 0

Element 9 is 0

Element 10 is 0

The output is normal. Therefore, if we use vctor <bool>, we should not use the following method for access. We should use the iterator or other functions provided by the vector, to avoid unexpected results, my program started to use subscript for access, causing me to debug a bug for several hours.

In the C ++ document, the introduction to vector <bool> is as follows (details stamp here ):

It behaves like the unspecialized versionof vector, with thefollowing changes:

The storage is not necessarily an arrayof bool values, but the library implementation may optimize storageso that each value is stored in a single bit.

Elements are not constructed usingthe Allocator object, but their value is directly set on the proper bit in the internal storage.

Member function flip and a newsignature for member swap.

A special member type, reference, Aclass that accesses individual bits in the container's internal storage with aninterface that emulates a bool reference. Conversely,
Membertype const_reference is a plain bool.

The pointer and iterator types used by thecontainer are not necessarily neither pointers nor conforming iterators, although they shall simulate most of their expected behavior.

However, vector <bool> is not recommended for Clause 18 in objective STL. I will not elaborate on it here. I can find a lot of explanations on the Internet, but I personally think that as long as I understand the defects of this container, in the process of use, it can still be used, for example, my current program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.