bitset
class template
<bitset>
A bitset is a special container class that is designed to store bits (elements with only two possible values: 0 or 1, true or false, ...).
The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char).
Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset[3] accesses its fourth bit, just like a regular array accesses its elements.
Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements:
Apart from overriding several operators and to provide direct access to the bits, bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see constructor, bitset::to_ulong and bitset::to_string). They can also be directly inserted and extracted from streams in binary format.
Bitsets have a fixed size. For a similar container class that also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of vector (vector<bool>).
In their implementation in the C++ Standard Template Library, bitsets take a single template parameter:
Where the template parameter has the following meaning:
Bit access:
Bit operations:
Bitset operations:
The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char).
Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset[3] accesses its fourth bit, just like a regular array accesses its elements.
Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements:
1 2 3 4 5 6 7 8 9 10 11 |
|
Apart from overriding several operators and to provide direct access to the bits, bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see constructor, bitset::to_ulong and bitset::to_string). They can also be directly inserted and extracted from streams in binary format.
Bitsets have a fixed size. For a similar container class that also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of vector (vector<bool>).
In their implementation in the C++ Standard Template Library, bitsets take a single template parameter:
|
Where the template parameter has the following meaning:
- N: Number of bits to contain (size_t is an integral type).
Member functions
(constructor) | Construct bitset (public member function) |
applicable operators | Bitset operators (functions) |
Bit access:
operator[] | Access bit (public member function) |
Bit operations:
set | Set bits (public member function) |
reset | Reset bits (public member function) |
flip | Flip bits (public member function) |
Bitset operations:
to_ulong | Convert to unsigned long integer (public member function) |
to_string | Convert to string (public member function) |
count | Count bits set (public member function) |
size | Return size (public member function) |
test | Return bit value (public member function) |
any | Test if any bit is set (public member function) |
none | Test if no bit is set (public member function) |
Không có nhận xét nào:
Đăng nhận xét