bitset::reference

提供为位引用的代理类在用于访问和操作的 bitset 包含一个单独的帮助程序类 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 的 Cos(angle) 位位置引用。 在 bitset . X。

返回值

将位的引用参数中指定的位置 bitset 为第一秒,第五和类的引用成员函数和 truefalse,反映已修改的位的值。bitset 的类引用的第三个和第四个成员函数中。

备注

引用类只能作为 bitset **operator[]的帮助程序类。 描述类成员可以访问在 bitset 内的单个位的对象。 允许 N>**为 **bitset<**类型并在此类对象中 一个 和第 j 的有效位置 bool类型,xy 的对象。 x [i] 表示法引用 i 在位位置 x。 类的引用成员函数提供顺序,以下操作:

Operation

定义

x[i=] b

存储 bool 值 b 在数位位置 在 bitset . X。

x[i=] yj[]

存储位 y[j] 数位位置值 i in bitset x

b ~x [i]

存储位 x[i 的Cos(angle) 翻转的 bool 值在b. 的。

b = x[i]

存储位 x[i] 在 bool b

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 类

C++ 标准库中的线程安全