Reproduced in: http://www.cnblogs.com/wpcockroach/p/3179572.html
Vector <t> the standard library template class should be a class that is frequently used by most C ++ programmers. However, vector <bool> may not be understood by programmers. It is not easy to know the problems contained in vector <bool>.
First, make it clear that vector <bool> is a special version of vector <t>. This special version solves the problem of storage capacity.
To optimize space allocation, a specialization of vector for bool elements is provided.
Therefore, it stores bool values in bits. From this we can see that if you use a bit to improve space efficiency, the problem may be time efficiency. Because our computer addresses are in bytes. Here is an example:
int main(){ clock_t t1, t2, t3; vector<bool> vb; vector<int> vi; vector<char> vc; t1 = clock(); for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) { vb.push_back(true); } } t1 = clock() - t1; t2 = clock(); for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) { vi.push_back(1); } } t2 = clock() - t2; t3 = clock(); for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) { vc.push_back('a'); } } t3 = clock() - t3; cout << "'vector<bool>::push_back(true)' 1000000 times cost: " << t1 << endl; cout << "'vector<int>::push_back(1)' 1000000 times cost: " << t2 << endl; cout << "'vector<char>::push_back('a')' 1000000 times cost: " << t3 << endl; t1 = clock(); for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) { bool b = vb[j + 1000 * i]; } } t1 = clock() - t1; t2 = clock(); for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) { int b = vi[j + 1000 * i]; } } t2 = clock() - t2; t3 = clock(); for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) { char b = vc[j + 1000 * i]; } } t3 = clock() - t3; cout << "'vector<bool>::operator[]' 1000000 times cost: " << t1 << endl; cout << "'vector<int>::operator[]' 1000000 times cost: " << t2 << endl; cout << "'vector<char>::operator[]' 1000000 times cost: " << t3 << endl; return 0;}
The output result on my machine is:
The time consumed by the vector <bool> is at least 40 times longer than that of the vector <t>! Not simple...
Here we just have to verify the special feature of vector <bool>. Next, let's take a look at what is special in the standard document:
There is no requirement that the data be stored as a contiguous allocation of bool values. A space-optimized representation of BITs is recommended instead.
Therefore, vector <bool> does not require that the underlying storage must be continuous. This illustrates one reason for the big difference in the above example.
In addition, vetor <bool> is actually a container from the exposed API. In fact, he is not a container.
Let's talk about the C ++ container first. We know that vector <t> (t is not bool) is a sequential container (sequence container ). Therefore, it meets the container standards in the C ++ standard. Let's say the following:
Expression |
Return type |
X: Reference |
Lvalue of T |
To put it simply, vector <int>: reference must be at least one Int. This is not the case for vector <bool>. Vector <bool>: Reference is a proxy data structure compatible with bool.
The simplest example is:
template <typename T>void func(vector<T>& v){ T& ref = v[0];}
In the above Code, you pass a vector <bool> object on vc2012 or G ++ 4.6.1, and neither of them can be compiled. The compilation error given by G ++ is quite clear:
Vectorbool. cpp: In function 'void func (STD: vector <t> &) [with T = bool ']':
Vectorbool. cpp: 18: 11: instantiated from here
Vectorbool. CPP: 9: 17: Error: Invalid initialization of non-const reference of Type 'bool & 'from an rvalue of type' STD: vector <bool> :: reference {aka STD: _ bit_reference }'
The reference returned by operator [] is a right value, not an left value (lvalue of T ).
Therefore, vector <bool> is not a container.
Summary:
- Vector <bool> may have performance problems
- It is not a real container, so the use of template code may be problematic. If the problem could occur in the compiler, it would be nice to switch to the runtime, which is a little troublesome (vector <bool>: operator []
Misbehavior ?).
Reference:
- What you shoshould know about vector <bool>
- The fate of vector <bool> in C ++ 09
- Vector <bool>: more problems, better solutions