std::bitset<N>::reference

来自cppreference.com
< cpp‎ | utility‎ | bitset
 
 
 
 
class reference;

std::bitset 类包含 std::bitset::reference 作为可公开访问的嵌套类。此类用作允许用户与 bitset 的单个位交互的代理对象,因为标准 C++ 类型(如引用和指针)没有内建足够指定单个位的精度。

std::bitset::reference 的基本用途是提供能从 operator[] 返回的左值。

任何通过 std::bitset::referencebitset 的读或写潜在地读或写整个底层 bitset

成员函数

(构造函数)
构造引用
(公开成员函数)
(析构函数)
销毁引用
(公开成员函数)
operator=
将值赋给被引用位
(公开成员函数)
operator bool
返回被引用位
(公开成员函数)
operator~
返回翻转的被引用位
(公开成员函数)
flip
翻转被引用位
(公开成员函数)

std::vector<bool, Alloc>::reference::reference

reference( const reference& ) = default;
(C++11 起)
(C++20 起为 constexpr)

从另一引用构造引用。复制构造函数是隐式声明的。(C++11 前)

其他构造函数只能由 std::vector<bool, Alloc> 访问。

std::bitset<N>::reference::~reference

~reference();
(C++23 起为 constexpr)

销毁引用。

std::bitset<N>::reference::operator=

reference& operator=( bool x );
(1)(C++11 起为 noexcept)
(C++23 起为 constexpr)
reference& operator=( const reference& x );
(2)(C++11 起为 noexcept)
(C++23 起为 constexpr)

赋值给被引用位。

参数

x-要赋值的值

返回值

*this

std::bitset<N>::reference::operator bool

operator bool() const;
(C++11 起为 noexcept)
(C++23 起为 constexpr)

返回被引用位的值。

返回值

被引用位。

std::bitset<N>::reference::operator~

bool operator~() const;
(C++11 起为 noexcept)
(C++23 起为 constexpr)

返回被引用位的反。

返回值

被引用位的反。

std::bitset<N>::reference::flip

reference& flip();
(C++11 起为 noexcept)
(C++23 起为 constexpr)

翻转被引用位。

返回值

*this

示例

#include <bitset>
#include <iostream>
 
int main()
{
    std::bitset<4> bs{0b1110};
    std::bitset<4>::reference ref = bs[2];
 
    auto info = [&](int id)
    {
        std::cout << id << ") bs:" << bs << ";引用位:" << ref << '\n';
    };
 
    info(1);
    ref = false;
    info(2);
    ref = true;
    info(3);
    ref.flip();
    info(4);
    ref = bs[1]; // operator=( const reference& x )
    info(5);
 
    std::cout << "6) ~引用位:" << ~ref << '\n';
}

输出:

1) bs:1110;引用位:1
2) bs:1010;引用位:0
3) bs:1110;引用位:1
4) bs:1010;引用位:0
5) bs:1110;引用位:1
6) ~引用位:0

参阅

访问指定的位
(公开成员函数)