bit::vector
— Element Access
1constexpr bool element(std::size_t i) const;
2constexpr reference element(std::size_t i);
3constexpr bool operator[](std::size_t i) const;
constexpr reference operator[](std::size_t i);
4constexpr bool operator()(std::size_t i) const;
constexpr reference operator()(std::size_t i);
5constexpr bool test(std::size_t i) const;
6constexpr bool front() const;
constexpr reference front()
7constexpr bool back() const;
constexpr reference back()
- 1
-
Accesses the value for bit-vector element
i
. - 2
-
Returns a
vector::reference
object — allows modification of the value at indexi
. - 3
-
The
operator[]
methods are synonyms for theelement
methods. - 4
-
The
operator()
methods are also synonyms for theelement
methods. - 5
-
Another way to access the value for element
i
. - 6
-
Access the element at index
0
. - 7
-
Access the element at index
size() - 1
.
Generally, these methods do not check whether the index i is in bounds. The behaviour is undefined if it is out of bounds, but it will surely not be good! Set the BIT_VERIFY flag at compile time to check this condition. If it is violated, the program will abort with a helpful message.
|
Example
#include <bit/bit.h>
int main()
{
std::size_t n = 11;
::vector<> v(n);
bitstd::cout << "Setting successive bits:\n";
std::cout << v << '\n';
for (std::size_t i = 0; i < n; ++i) {
[i] = true;
vstd::cout << v << '\n';
}
std::cout << "Resetting the front and back elements of v ...\n";
.front() = 0;
v.back() = 0;
vstd::cout << v << '\n';
std::cout << "v.front(): " << v.front() << '\n';
std::cout << "v.back(): " << v.back() << '\n';
}
Output
Setting successive bits:
[0 0 0 0 0 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0 0 0 0]
[1 1 0 0 0 0 0 0 0 0 0]
[1 1 1 0 0 0 0 0 0 0 0]
[1 1 1 1 0 0 0 0 0 0 0]
[1 1 1 1 1 0 0 0 0 0 0]
[1 1 1 1 1 1 0 0 0 0 0]
[1 1 1 1 1 1 1 0 0 0 0]
[1 1 1 1 1 1 1 1 0 0 0]
[1 1 1 1 1 1 1 1 1 0 0]
[1 1 1 1 1 1 1 1 1 1 0]
[1 1 1 1 1 1 1 1 1 1 1]
Resetting the front and back elements of v ...
[0 1 1 1 1 1 1 1 1 1 0]
v.front(): 0
v.back(): 0