Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behaviour of std::array<bool> vs. std::vector<bool>

Tags:

c++

std

c++11

Does the std::array<bool> implement the same bit packing memory optimisation that std::vector<bool> does?

Thanks!

like image 373
Clio Callida Avatar asked Jun 17 '17 17:06

Clio Callida


People also ask

What is the difference between std :: array and std::vector?

Difference between std::vector and std::array in C++Vector is a sequential container to store elements and not index based. Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements.

Is std::vector slower than array?

A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.

Should I use arrays or vectors in C++?

Vector is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. Vector occupies much more memory in exchange for managing storage and growing dynamically, whereas Arrays are a memory-efficient data structure.

What is a vector Boolean?

The vector<bool> class is a partial specialization of vector for elements of type bool . It has an allocator for the underlying type that's used by the specialization, which provides space optimization by storing one bool value per bit.


1 Answers

No, std::array has no specialization for bool type.

You can find more details here, but, basically, std::array is just a:

an aggregate type with the same semantics as a struct holding a C-style array T[N]

and in case of bool you might consider it as a C-style array of bools, not any kind of bitset.

like image 183
Edgar Rokjān Avatar answered Nov 28 '22 13:11

Edgar Rokjān