bitset::reference
Une classe proxy qui fournit des références aux bits est contenu dans un bitset utilisé pour accéder et manipuler des différents bits comme une classe d'assistance pour operator[] de bitset de classe.
class reference {
friend class bitset<N>;
public:
reference& operator=(
bool _Val
);
reference& operator=(
const reference& _Bitref
);
bool operator~( ) const;
operator bool( ) const;
reference& flip( );
};
Paramètres
_Val
La valeur de l'objet du type bool à assigner à un bit dans un bitset._Bitref
Une référence de la forme X I [] au bit à la position i dans le bitset X.
Valeur de retour
Une référence au bit dans le bitset spécifié par position d'argument pour la première seconde, et cinquièmes fonctions membres de classe, et true ou false, pour refléter la valeur de bits modifié dans le bitset pour le troisième et le quatrième fonctions membres de la référence de classe.
Notes
La référence de classe existe uniquement comme classe d'assistance pour le bitset operator[].La classe membre décrit un objet qui peut accéder à un bit individuel dans un bitset.Laissez b être un objet d'objets de type bool, x et y de type bitset<N>, et de caractères valides d'I et de j dans un tel objet.La notation X I [] référence le bit à la position i dans le bitset *X.*Les fonctions membres de la référence de classe fournissent, dans l'ordre, les opérations suivantes :
Opération |
Définition |
---|---|
XI= [] b |
Valeur b d' bool de mémoire à la position de bits i dans le bitset X. |
XI= [] [] yj |
Stocke la valeur de bits est[]jà la position de bits i dans le bitset X. |
b ~xI= [] |
Stocke la valeur retournée de bits XI[] dans boolB. |
bxI= [] |
Stocke la valeur de bits XI[] dans boolB. |
XI[].flip() |
Stocke la valeur retournée du changements de bits X[]Ià la position de bits i dans le X. |
Exemple
// 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 << ".";
}
Configuration requise
en-tête : <bitset>
l'espace de noms : DST