A bit_vector is more like a vector < bool >
View versions (1)
Definition
Bit_vector is defined in the standard header <vector>, and in the nonstandard backward-compatibility header <bvector.h>.
Description
A bit_vector is like a vector<bool>. The difference between the two is that a vector requires at least one byte per element, but a bit_vector only requires one bit per element.
Short example of using bit_vector:
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;
If you need a container for a variable number of bits or Boolean values, you can use the class bit_vector, otherwise if you need a bitfield with static size, you should use http://codecogs.izyba.com/reference/computing/containers/bitset.php"bitset" .
Bit_vector Operations
| Operation | Effect |
| c.flip() | Negates all Boolean elements (complement of all bits) |
| m[i].flip() | Negates the Boolean element with index i (complement of a single bit) |
m[i]=val | Assigns val to the Boolean element with index i (assignment to a single bit) |
| m[i1] = m[i2] | Assigns the value of the element with index i2 to the element with index i1 |
References
Example 1
ProblemThis example of program illustrates examples of implicit casts done by the ostream class to a variable of bit_vector.
Workings#include <vector>
#include <iostream>
using namespace std;
int main( )
{
bit_vector v;
v.push_back(false);
bit_vector::reference ref1=v.at(0);
cout <<ref1<<endl; // ref1 implicitly cast to bool
bool b1;
// one form of an explicit cast
b1=ref1.operator bool( );
cout <<b1<<endl;
// another form of an explicit cast
b1=bool(ref1);
cout <<b1<<endl;
return 0;
}
Example 2
ProblemThis program illustrates some simple operations using a bit_vector.
Workings#include <vector>
#include <iostream>
using namespace std;
int main ()
{
// instantiate a bit_vector to hold 3 elements
bit_vector v(3);
// assign 3 elements using the array operator []
v[0] = true;
v[1] = true;
v[2] = false;
// insert a 4th element using push_back:
// this will cause the vector to resize the buffer
v.push_back (true);
cout <<"The contents of the vector are: "<<endl<<"{";
for (size_t i = 0; i < v.size(); ++i)
cout <<v[i]<<' ';
cout <<"}"<<endl<<endl;
v.flip();
cout <<"The flipped contents of the vector are: "<<endl<<"{";
for (size_t i = 0; i < v.size(); ++i)
cout <<v[i]<<' ';
cout <<"}";
return 0;
}
SolutionOutput:
The contents of the vector are:{1 1 0}
The flipped contents of the vector are:{0 1 1}
Example 3
Workings#include <iostream>
#include <vector>
using namespace std;
const SIZE=4;
int main()
{
// dynamically allocated vector initially contains no elements
bit_vector v(4);
for (int i=0; i<SIZE; i++)
if (i>1)
v.push_back(i-2);
else
v.push_back(i);
cout <<"First element is: "<<v.front()<<"."<<endl;
cout <<"Last element is: "<<v.back()<<"."<<endl;
cout <<"The vector contains "<<v.size()<<" elements."<<endl;
cout <<"Erase the last element."<<endl;
v.erase(v.end()-1); // erase the last element in the vector
cout <<"The new last element is "<<v.back()<<"."<<endl;
cout <<"Erase the first element."<<endl;
v.erase(v.begin()); // erase the first element in the vector
cout <<"The new first element is "<<v.front()<<"."<<endl;
cout <<"The vector now contains "<<v.size()<<" elements."<<endl;
return 0;
}
SolutionOutput:
First element is: 0
Last element is: 1
The vector contains 4 elements.
Erase the last element.
The new last element is 0.
Erase the first element.
The new first element is 1.
The vector now contains 2 elements.
References