Share via


bitset::reference

提供以位元組的所參考的 Proxy 類別是用來存取和操作的 bitset 包含了兩個用來當做 Helper 類別 bitset operator[] 分類。

class reference {
   friend class bitset<N>;
   public:
      reference& operator=(
         bool _Val
      );
      reference& operator=(
         const reference& _Bitref
      );
      bool operator~( ) const;
      operator bool( ) const;
      reference& flip( );
};

參數

  • _Val
    型別會指定的 bool 物件的值指派給 bitset 的位元。

  • _Bitref
    表單 X i [] 參考以位元組在位置 在 bitset x。

傳回值

以位元組的參考在引數中指定的位置 bitset 以第一秒,和類別參考的第五 + 成成員函式和 truefalse,反映修改之位元的值。bitset 的類別參考的第三個和第四個成員函式的。

備註

類別參考只能做為 bitset 的 **operator[]**Helper 類別。 描述類別成員都可以存取 bitset 內的個別位元的物件。 讓 b 屬於型別 **bitset<N>**和在這類物件內的 ij 有效位置類型, boolxy 物件。 標記法 x [i] 所 參考的位元位置 在 bitset x。 類別參考的成員函式的順序,提供,下列作業:

作業

定義

x[i] = b

儲存 bool 值 b 在位元位置 在 bitset x。

x[i] = Y[]

儲存位元 y的[] 值在位元位置 在 bitset x。

b = ~x [i]

在 boolb.Xi位元儲存 翻轉的值

b = x[i]

在 boolb.儲存位元 Xi 的[] 值。

x[i]。flip()

儲存位元 x[i] 後面的翻轉的值在位元位置 x。

範例

// bitset_reference.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 2 );
   bitset<5> b2 ( 6 );
   cout << "The initialized bitset<5> b1( 2 ) is: ( "<< b1 << " )."
        << endl;
   cout << "The initialized bitset<5> b2( 6 ) is: ( "<< b2 << " )."
        << endl;

   // Example of x [i] = b storing bool b at bit position i
   // in bitset x
   b1[ 0 ] = true;
   cout << "The bitset<5> b1 with the bit at position 0 set to 1"
        << " is: ( "<< b1 << " )" << endl;
   
   // Example of x [i] = y [j] storing the bool value of the
   // bit at position j in bitset y at bit position i in bitset x
   b2 [4] = b1 [0];      // b1 [0] = true
   cout << "The bitset<5> b2 with the bit at position 4 set to the "
        << "value\n of the bit at position 0 of the bit in "
        << "bitset<5> b1 is: ( "<<  b2  << " )" << endl;

   // Example of b = ~x [i] flipping the value of the bit at
   // position i of bitset x and storing the value in an 
   // object b of type bool
   bool b = ~b2 [4];      // b2 [4] = false
   if ( b )
      cout << "The value of the object b = ~b2 [4] "
           << "of type bool is true." << endl;
   else
      cout << "The value of the object b = ~b2 [4] "
           << "of type bool is false." << endl;
   
   // Example of b = x [i] storing the value of the bit at
   // position i of bitset x in the object b of type bool
   b = b2 [4];
   if ( b )
      cout << "The value of the object b = b2 [4] "
           << "of type bool is true." << endl;
   else
      cout << "The value of the object b = b2 [4] "
           << "of type bool is false." << endl;

   // Example of x [i] . flip ( ) toggling the value of the bit at
   // position i of bitset x
   cout << "Before flipping the value of the bit at position 4 in "
        << "bitset b2,\n it is ( "<<  b2  << " )." << endl;
   b2 [4].flip( );
   cout << "After flipping the value of the bit at position 4 in "
        << "bitset b2,\n it becomes ( "<<  b2  << " )." << endl;
   bool c;
   c = b2 [4].flip( );
   cout << "After a second toggle, the value of the position 4"
        << " bit in b2 is now: " << c << ".";
}
  
  
  
  
  
  
  

需求

標題: <bitset>

命名空間: std

請參閱

參考

bitset Class

在標準 C++ 程式庫中的執行緒安全