A
bit_vector is essentially a
vector<bool>: it is a
Sequence that has the same interface as
vector. The main difference is that
bit_vector is optimized for space efficiency. A
vector always requires at least one byte per element, but a
bit_vector only requires one bit per element.
the function swap has another usage ,you can use it to free the memory it occupied ,by swap it with a empty vector , if the vector is not created on heap .
vector<class T> a .......
{
vector<class T> b();
swap(a,b);
}
Warning: The name bit_vector will be removed in a future release of the STL. The only reason that bit_vector is a separate class, instead of a template specialization of vector<bool>, is that this would require partial specialization of templates. On compilers that support partial specialization, bit_vector is a specialization of vector<bool>. The name bit_vector is a typedef. This typedef is not defined in the C++ standard, and is retained only for backward compatibility.
Example
bit_vector V(5);//
V[0] = true;
V[1] = false;
V[2] = false;
V[3] = true;
V[4] = false;
for (bit_vector::iterator i = V.begin(); i < V.end(); ++i)
cout << (*i ? '1' : '0');
cout << endl;
void reserve(size_type n) | If n is less than or equal to capacity(), this call has no effect. Otherwise, it is a request for the allocation of additional memory. If the request is successful, then capacity() is greater than or equal to n; otherwise, capacity() is unchanged. In either case, size() is unchanged. [2] [4] |
void swap(bit_vector::reference x, | Swaps the bits referred to by x and y. This is a global function, not a member function. It is necessary because the ordinary version of swap takes arguments of type T&, and bit_vector::reference is a class, not a built-in C++ reference. |
the function swap has another usage ,you can use it to free the memory it occupied ,by swap it with a empty vector , if the vector is not created on heap .
vector<class T> a .......
{
vector<class T> b();
swap(a,b);
}