This is part of my weekly C++ posts based on the daily C++ tips I make at my work. I strongly recommend this practice. If you don't have it in your company start it.
List of all weekly posts can be found here.
1. bitset
std::bitset is a container for bits. Unlike the infamous
std::vector<bool> it size is set at compile time so bitsets with different sizes are different types. It is designed to replace C-style more-<<,>>,|,&-than-program-expressions storing of binary flags in a long for example. It has
operator[],
bound checking
tests, evaluating if all, none or any of the bits are
true. It can also count the bits set to
true so if are asked during an interview "How to count the true bits in a sequence of chars?" you can show the depth of your STL knowledge by answering something like "Well first I'll use
std::bitset::count to write the most readable solution, then use it for baseline and then use std::array<char, 256> with precomputed number of bits for each index"