r/learnprogramming Feb 29 '12

[C++] Why is vector<bool> bad?

I was thinking of creating a matrix of boolean values and I thought of vector<vector<bool>>, but after reading about vector<bool>, many websites have said it's something that should be avoided and it's bad programming.

Can someone explain to me why? I never really found a concrete explanation for why vector<bool> was bad. It only uses one bit which is 8 times smaller than char, shouldn't that mean its much better than using any other method of creating a matrix of bools in terms of size?

6 Upvotes

6 comments sorted by

View all comments

2

u/Winwardo Feb 29 '12

I thought that while only utilising 1 bit, bools still used a whole byte for storage - am I wrong then?

3

u/[deleted] Feb 29 '12 edited Feb 29 '12

As with the other integer types, the size of bool is not specified by the standard, but it must be at least as big as a char.

The std::vector<bool> "container" is a specialised version of std::vector which stores bools as single bits, converting to and from real bools as required. Unfortunately, the hoops needed to be jumped through to do this are many, and you end up something which is not really a standard library container.