What is the best way to store an array of boolean values using the following containers from STL for maximum performance?
Explanation
Specialization vector<bool> stores values in bits, not in bool variables. Since there are no pointers and references to bits in C++, elements of vector<bool> are not addressable. Also vector<bool>::iterator is not a random access iterator, unlike vector<string>::iterator or vector<char>::iterator. This causes the following code to fail:
template <typename T> void func(vector<T> & vec)
{
 T & ref=vec.front(); //works with all types besides bool
}
Therefore, the best is to use vector<char>.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes