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 elementj
.
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()
{
::vector<> v(2);
bit(0) = 0; v(1) = 1;
vstd::cout << "Before swap v = " << v << "\n";
.swap_elements(0,1);
vstd::cout << "After swap v = " << v << "\n";
}
Output
Before swap v = [0 1]
After swap v = [1 0]