Sdílet prostřednictvím


bitset::reference

Proxy třídy, která obsahuje odkazy na bitů obsažených v bitset, používaný přistupovat a manipulovat s jednotlivými bity jako pomocná třída pro operator[] z třídy bitset.

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

Parametry

  • _Val
    Hodnota objekt typu bool má být přiřazen trochu v bitset.

  • _Bitref
    Odkaz formulář x [i bitů na pozici i v bitset x.

Vrácená hodnota

Odkaz na bit v bitset určené pozice argument pro první, druhý a pátý členské funkce tříd, a true nebo false, aby odrážel hodnotu změněné bit bitset pro třetí a čtvrtý členské funkce tříd.

Poznámky

Referenční třídy existuje pouze jako pomocná třída pro bitset operator[].Člen třídy popisuje objekt, který lze přístup jednotlivé bit v rámci hodnoty bitset.Nechť b být objekt typu bool, x a y objekty typu bitset<N>, a i a j platné pozice v takový objekt.Zápis x [i odkazuje na bit na pozici i v bitset x.Členské funkce tříd poskytují v pořadí, následující operace:

Operace

Definice

xi= b

Obchody bool hodnoty b na pozici bit i v bitset x.

xi= yj

Uloží hodnotu bit yjna pozici bit i v bitset x.

b = ~xi

Uloží hodnotu převrácený bit xi vboolb

b = xi

Stores the value of the bit xi inbool b.

xi.flip( )

Uloží hodnotu převrácený bit xizpět na pozici bit i v x.

Příklad

// 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 << ".";
}
  

Požadavky

Záhlaví:<bitset>

Obor názvů: std

Viz také

Referenční dokumentace

bitset – třída

Bezpečný přístup z více vláken ve standardní knihovně C++