bit::vector — Change the Capacity
Potentially change the vector::capacity of a bit-vector.
- 1
-
Increases the bit-vector’s
vector::capacityto holdnelements.
Does nothing ifnelements fit inside the current capacity. - 2
-
This is a request to minimize the unused/excess
vector::capacity. May do nothing.
The idea is to make it as efficient as possible to append a (known) large number of elements to a bit-vector by allocating the needed storage up-front rather than in pieces.
These methods return *this, so you can chain them with other calls.
These methods do not change a bit-vector’s size(). No elements are added or deleted.
| If the capacity changes, all the old values remain unchanged. However, any references are then invalidated. |
Example
#include <bit/bit.h>
int main()
{
auto v = bit::vector<>::checker_board(8);
1 std::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n';
2 v.reserve(99);
3 std::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n';
4 v.shrink_to_fit();
std::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n';
}- 1
-
We’re using the default 64-bit blocks, so
vcan hold 64 elements (though it only has eight at present). - 2
-
Get
v“ready” to hold 99 elements. - 3
- As the blocks are all 64-bits, we need two for those 99 elements; therefore, the capacity increases to 128.
- 4
-
We changed our minds and want to shrink
vto a minimum size. Note that the elements invnever changed!
Output
[1 0 1 0 1 0 1 0]: size 8, capacity 64
[1 0 1 0 1 0 1 0]: size 8, capacity 128
[1 0 1 0 1 0 1 0]: size 8, capacity 64