+ 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

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.

Hot today

Free month of PRO subscription

9 Votes

Please help me with HTML

0 Votes

Hash in python

1 Votes

Unable to overload +

2 Votes

Package of the Java

0 Votes

What is the mistake here?

2 Votes

Hey! [SOLVED]

0 Votes

Adding word

1 Votes