bit::vector — Extract a Sub-Vector
We have methods to extract a sub-vector as a stand-alone, distinct copy of elements from this bit-vector.
1constexpr bit::vector sub(std::size_t begin, std::size_t len) const;
2constexpr bit::vector sub(int len) const;- 1
-
Returns a bit-vector of size
len, a copy of the elements starting atbegin. - 2
-
Returns a copy of the first
lenelements iflen > 0or the finalabs(len)elements iflen < 0.
begin has to be a valid index, and abs(len) elements must be available for copying. Set the BIT_VERIFY flag at compile time to check this condition — any violation will cause the program to abort with a helpful message.
|
Example
#include <bit/bit.h>
int main()
{
1 auto v = bit::vector<>::random(12);
std::cout << "v: " << v << "\n";
2 std::cout << "v.sub(0, 4): " << v.sub(0, 4) << "\n";
3 std::cout << "v.sub(4): " << v.sub(4) << "\n";
4 std::cout << "v.sub(-4): " << v.sub(-4) << "\n";
5 std::cout << "v.sub(8, 4): " << v.sub(8, 4) << "\n";
}- 1
- Construct a vector of size 12 with a random fill.
- 2
- Extract four elements starting at index 0.
- 3
- Do the same thing using a shorthand notation.
- 4
- Extract the final four elements using the shorthand notation.
- 5
- Do the same thing by copying four elements starting at index 8.
Output
v: [0 1 0 0 1 0 1 0 0 1 1 0]
v.sub(0, 4): [0 1 0 0]
v.sub(4): [0 1 0 0]
v.sub(-4): [0 1 1 0]
v.sub(8, 4): [0 1 1 0]