bit::vector
— Change the Capacity
Potentially change the vector::capacity
of a bit-vector.
- 1
-
Increases the bit-vector’s
vector::capacity
to holdn
elements.
Does nothing ifn
elements 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);
1std::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n';
2.reserve(99);
v3std::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n';
4.shrink_to_fit();
vstd::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n';
}
- 1
-
We’re using the default 64-bit blocks, so
v
can 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
v
to a minimum size. Note that the elements inv
never 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