名稱空間
變體
操作

std::bitset<N>::reference

來自 cppreference.com
< cpp‎ | 工具庫‎ | bitset
 
 
 
 
class reference;

std::bitset 類包含 std::bitset::reference 作為可公開訪問的巢狀類。此類的作用是代理物件,允許使用者與 bitset 的單個位進行互動,因為標準 C++ 型別(如引用和指標)的精度不足以指定單個位。

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

透過 std::bitset::reference 對 bitset 進行的任何讀寫操作都可能讀寫整個底層 bitset。

目錄

[編輯] 成員函式

(建構函式)
構造引用
(公開成員函式)
(解構函式)
銷燬引用
(公開成員函式)
operator=
為被引用位賦值
(公開成員函式)
operator bool
返回被引用位
(公共成員函式) [編輯]
operator~
返回被引用位的反轉值
(公開成員函式)
flip
翻轉被引用位
(公開成員函式)

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

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

從另一個引用構造引用。 複製建構函式被隱式宣告。(C++11 前)

其他建構函式只能由 std::bitset 訪問。

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

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

銷燬引用。

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

reference& operator=( bool x );
(1) (C++11 起無異常丟擲)
(C++23 起為 constexpr)
reference& operator=( const reference& x );
(2) (C++11 起無異常丟擲)
(C++23 起為 constexpr)

為被引用位賦值。

引數

x - 要賦的值

返回值

*this

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

operator bool() const;
(C++11 起無異常丟擲)
(C++23 起為 constexpr)

返回被引用位的值。

返回值

被引用位。

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

bool operator~() const;
(C++11 起無異常丟擲)
(C++23 起為 constexpr)

返回被引用位的反轉值。

返回值

被引用位的反轉值。

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

reference& flip();
(C++11 起無異常丟擲)
(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 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

[編輯] 參閱

訪問特定位
(公共成員函式) [編輯]