+ 2

Why capacity changes for bool but not for int

Please refer code below : Capacity and size should be same for vector as I just have initialized data. When checked size and capacity for vector of int, it is three as expected This is not true for bool.... Why am I getting size as 64 intead of 3 for vBool ? https://code.sololearn.com/cNBnY9fKP1Bo/?ref=app

12th Jul 2022, 9:57 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 1
Hm, interesting. I did some research and it seems that vector<bool> is a specialized template that is stored differently than other vectors. 1 boolean in C++ is actually 8 bit long for technical reasons. However, vector is a special structure optimized to store 1 boolean value per bit, so it can store 8 as many boolean values. The structure used internally to solve this is 64 bits large, so the capacity of vector will always be a multiple of 64.
12th Jul 2022, 10:34 PM
Chris - avatar