Bit Vector
A bit_vector is more like a vector < bool >
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 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
- http://www.sgi.com
- Nicolai M. Josuttis: "The C++ Standard Library"
- http://www.java2s.com
- http://msdn.microsoft.com/en-us
- Kris Jamsa, Lars Klander: "Jamsa's C/C++ Programmer's Bible"
CodeCogs®