命名空间
变体
操作

std::bitset<N>::reference

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

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

std::bitset::reference 的主要用途是提供一个可以从 operator[] 返回的左值。

通过 std::bitset::reference 对位集进行的任何读取或写入操作都可能读取或写入整个底层位集。

目录

[编辑] 成员函数

(构造函数)
构造引用
(公共成员函数)
(析构函数)
销毁引用
(公共成员函数)
operator=
为引用的位赋值
(公共成员函数)
operator bool
返回引用的位
(公共成员函数) [编辑]
operator~
返回反转的引用位
(公共成员函数)
flip
反转引用的位
(公共成员函数)

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

reference( const reference& ) = default;
(自 C++11 起)
(constexpr 自 C++23 起)

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

其他构造函数只能由 std::bitset 访问。

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

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

销毁引用。

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

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

为引用的位赋值。

参数

x - 要赋值的值

返回值

*this

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

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

返回引用的位的值。

返回值

引用的位。

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

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

返回引用位的反转值。

返回值

引用位的反转值。

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

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

反转引用的位。

返回值

*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 bit: " << 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 bit: " << ~ref << '\n';
}

输出

1) bs: 1110; ref bit: 1
2) bs: 1010; ref bit: 0
3) bs: 1110; ref bit: 1
4) bs: 1010; ref bit: 0
5) bs: 1110; ref bit: 1
6) ~ref bit: 0

[编辑] 参见

访问特定位
(公共成员函数) [编辑]