bitset::reference
Прокси-класс, ссылки на битам содержал в bitset, которое используется для получения и обработки отдельных биты как вспомогательный класс для operator[] 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( );
};
Параметры
_Val
Значение объекта типа bool, чтобы присвоить бита в bitset._Bitref
Ссылка формы x [ i ] в бит на позицию i в bitset x.
Возвращаемое значение
Ссылка на bitset биту в определенном позиции аргумента в первый, второй и ссылки пятые функции-члены класса и true или false, его значение измененного бита в bitset для третьего и ссылки четвертых функции-члены класса.
Заметки
Справочные сведения о классах существует только как вспомогательный класс для bitset operator[]. Класс члена описание объекта, позволяющий извлечь отдельные бит в bitset. Включение B является объектом типа bool, x и y объектов типа bitset<N> и положений I и j допустимых в таких объектов. Запись x [i] ссылается на позицию i в bitset x. Функции-члены класса предоставляют ссылки в порядке, следующие операции:
Операция |
Определение |
---|---|
x[i] = b |
Хранит bool значение B в позиции битов i в bitset x. |
x[i] = y[j] |
Сохраняет значение бита y[j] в позиции битов i в bitset x. |
b = ~x[i] |
Хранит перевернутое значение бита x[i] в bool b. |
b = x[i] |
Сохраняет значение бита x[i] в bool b. |
x[i].flip() |
Хранит значение перевернутое обратно в x[i] в позиции битов 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