bit::vector — Swap Elements

We have a method to swap the values of two individual elements/bits in a bit-vector.

1constexpr bit::vector &swap(std::size_t i, std::size_t j) const;
1
Swaps the values at element i and element j.
By default, the method does not check whether the indices are in bounds, and if they aren’t, the behaviour is undefined (but bound to be bad)!
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()
{
    bit::vector<> v(2);
    v(0) = 0; v(1) = 1;
    std::cout << "Before swap v = " << v << "\n";
    v.swap_elements(0,1);
    std::cout << "After  swap v = " << v << "\n";
}

Output

Before swap v = [0 1]
After  swap v = [1 0]

See Also

vector::swap

Back to top