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 index i.
3
The operator[] methods are synonyms for the element methods.
4
The operator() methods are also synonyms for the element 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;
    bit::vector<> v(n);
    std::cout << "Setting successive bits:\n";
    std::cout << v << '\n';
    for (std::size_t i = 0; i < n; ++i) {
        v[i] = true;
        std::cout << v << '\n';
    }
    std::cout << "Resetting the front and back elements of v ...\n";
    v.front() = 0;
    v.back()  = 0;
    std::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

See Also

vector::reference
vector::size
bit_verify

Back to top