Klasa bitset

Opisuje typ obiektu, który przechowuje sekwencję składającą się z stałej liczby bitów, które zapewniają kompaktowy sposób utrzymywania flag dla zestawu elementów lub warunków. Klasa bitset obsługuje operacje na obiektach typu bitset, które zawierają kolekcję bitów i zapewniają stały dostęp do każdego bitu.

Składnia

template <size_t N>
class bitset

Parametry

N
Określa liczbę bitów w bitset obiekcie z niezerową liczbą całkowitą typu size_t , który musi być znany w czasie kompilacji.

Uwagi

W przeciwieństwie do podobnej vector<bool> klasybitset klasa nie ma iteratorów i nie jest kontenerem biblioteki standardowej C++. Różni się on również od vector<bool> tego, że jest określony rozmiar, który jest stały w czasie kompilacji zgodnie z rozmiarem określonym przez parametr N szablonu bitset<N> , gdy jest zadeklarowany.

Bit jest ustawiany, jeśli jego wartość to 1, a jeśli jej wartość to 0. Aby przerzucić lub odwrócić bit, należy zmienić jego wartość z 1 na 0 lub z 0 na 1. Bity N w obiekcie bitset są indeksowane przez wartości całkowite z zakresu od 0 do N -1, gdzie 0 indeksuje pierwszą pozycję bitową i N - 1 ostatnią pozycję bitową.

Członkowie

Konstruktory

Nazwa/nazwisko opis
bitset Tworzy obiekt klasy bitset<N> i inicjuje bity do zera, do określonej wartości lub wartości uzyskanych z znaków w ciągu.

Typedefs

Nazwa/nazwisko opis
element_type Typ, który jest synonimem typu bool danych i może służyć do odwołowania się do bitów elementów w obiekcie bitset.

Funkcje

Nazwa/nazwisko opis
all Testuje wszystkie bity w tym bitset celu, aby określić, czy wszystkie są ustawione na true.
any Funkcja składowa sprawdza, czy jakikolwiek bit w sekwencji jest ustawiony na 1.
count Funkcja składowa zwraca liczbę bitów ustawionych w sekwencji bitów.
flip Odwraca wartość wszystkich bitów w obiekcie bitset lub odwraca jeden bit w określonej pozycji.
none Sprawdza, czy bit nie został ustawiony na 1 w bitset obiekcie.
reset Resetuje wszystkie bity w bitset 0 lub resetuje bit w określonej pozycji do 0.
set Ustawia wszystkie bity na bitset wartość 1 lub ustawia bit w określonej pozycji na 1.
size Zwraca liczbę bitów w bitset obiekcie.
test Sprawdza, czy bit na określonej pozycji w bitset obiekcie ma wartość 1.
to_string Konwertuje bitset obiekt na reprezentację ciągu.
to_ullong Zwraca sumę wartości bitowych w bitset obiekcie jako unsigned long long.
to_ulong Konwertuje bitset obiekt na unsigned long obiekt, który wygeneruje sekwencję bitów zawartych, jeśli zostanie użyty do zainicjowania obiektu bitset.

Klasy

Nazwa/nazwisko opis
reference Klasa serwera proxy, która zawiera odwołania do bitów zawartych w obiekcie bitset używanym do uzyskiwania dostępu do poszczególnych bitów i manipulowania nimi jako klasy pomocniczej klasy operator[]bitset.

Operatory

Nazwa/nazwisko opis
operator!= Testuje element docelowy bitset pod kątem nierówności z określonym bitsetelementem .
operator&= Wykonuje bitową kombinację zestawów bitów z bitową operacją "and" (&).
operator<< Przesuwa bity w kierunku bitset po lewej stronie określoną liczbę pozycji i zwraca wynik na nową bitsetwartość .
operator<<= Przesuwa bity w lewo bitset określoną liczbę pozycji i zwraca wynik do docelowej bitset.
operator== Testuje element docelowy bitset pod kątem równości z określonym bitsetelementem .
operator>> Przesuwa bity w bitset prawo określoną liczbę pozycji i zwraca wynik na nową bitsetwartość .
operator>>= Przenosi bity w prawo bitset do określonej liczby pozycji i zwraca wynik do docelowej bitset.
operator[] Zwraca odwołanie do bitu na określonej pozycji w bitset obiekcie , jeśli bitset jest modyfikowalne. W przeciwnym razie zwraca wartość bitu w tej pozycji.
operator^= Wykonuje bitową kombinację zestawów bitów z bitową operacją "xor" (^).
operator|= Wykonuje bitową kombinację zestawów bitów z bitową operacją "or" (|).
operator~ Odwraca wszystkie bity w obiekcie docelowym bitset i zwraca wynik.

Struktury

Nazwa/nazwisko opis
hash

all

Sprawdza wszystkie bity w tym zestawie bitów, aby ustalić, czy wszystkie są ustawione na wartość true.

bool all() const;

Wartość zwracana

Zwraca wartość true , jeśli wszystkie bity w tym zestawie mają wartość true. Zwraca wartość false , jeśli co najmniej jeden bit ma wartość false.

any

Sprawdza, czy dowolny bit w sekwencji ma wartość 1.

bool any() const;

Wartość zwracana

true jeśli jakikolwiek bit w tym bitset elemecie jest ustawiony na 1; false jeśli wszystkie bity mają wartość 0.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   bool b, rb;

   cout << "The original bitset b1( 6 ) is: ( "<< b1 << " )"
        << endl;

   b = b1.any ( );

   if ( b )
      cout << "At least one of the bits in bitset is set to 1."
           << endl;
   else
      cout << "None of the bits in bitset are set to 1."
           << endl;

   bitset<5> rb1;
   rb1 = b1.reset ( );

   cout << "The reset bitset is: ( "<< b1 << " )"
        << endl;

   rb = rb1.any ( );

   if ( rb )
      cout << "At least one of the bits in the reset bitset "
           << "are set to 1." << endl;
   else
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
}
The original bitset b1( 6 ) is: ( 00110 )
At least one of the bits in bitset is set to 1.
The reset bitset is: ( 00000 )
None of the bits in bitset b1 are set to 1.

bitset

Tworzy obiekt klasy bitset<N> i inicjuje bity do zera lub do określonej wartości lub do wartości uzyskanych z znaków w ciągu.

1) constexpr bitset();
2) bitset(unsigned long val);
3) constexpr bitset(unsigned long long val);
4) template <class CharType, class Traits, class Allocator>
    explicit bitset(
        const basic_string<CharType, Traits, Allocator>& str,
        typename basic_string<CharType, Traits, Allocator>::size_type pos = 0);
    
5) template <class CharType, class Traits, class Allocator>
    explicit bitset(
        const basic_string<CharType, Traits, Allocator>& str,
        typename basic_string<CharType, Traits, Allocator>::size_type pos,
        typename basic_string<CharType, Traits, Allocator>::size_type count,
        CharType Zero = CharType ('0'),
        CharType One = CharType ('1'));
    
6) template<class CharType>
    explicit bitset(
        const CharType* str,
        typename basic_string<CharType>::size_type
        n = basic_string<CharType>::npos,
        CharType zero = CharType('0'),
        CharType one = CharType('1'));

Parametry

val
Niepodpisane liczby całkowite, których podstawowa druga reprezentacja jest używana do inicjowania bitów w bitset konstruowaniu.

str
Ciąg zer i tych używanych do inicjowania wartości bitowych bitset .

pos
Pozycja znaku w ciągu, licząc od lewej do prawej i rozpoczynająca się od zera, używana do inicjowania pierwszego bitu w obiekcie bitset.

count
Liczba znaków w ciągu używanym do podawania wartości początkowych bitów w obiekcie bitset.

Zero
Znak używany do reprezentowania zera. Wartość domyślna to "0".

One
Znak, który jest używany do reprezentowania jednego. Wartość domyślna to "1".

Uwagi

1) Tworzy obiekt klasy bitset<N> i inicjuje wszystkie N bitów do wartości domyślnej zero.

2-3) Tworzy obiekt klasy bitset<N> i inicjuje bity z parametru val .

4) Tworzy obiekt klasy bitset<N> i inicjuje bity z znaków podanych w ciągu zer i tych. Jeśli jakiekolwiek znaki ciągu są inne niż 0 lub 1, konstruktor zgłasza obiekt klasy invalid argument. Jeśli określona pozycja (pos) przekracza długość ciągu, konstruktor zgłasza obiekt klasy out_of_range. Konstruktor ustawia tylko te bity na pozycji j w bitset obiekcie, dla którego znak w ciągu na pozycji pos + j wynosi 1. Domyślnie pos wartość to 0.

5) Podobnie jak w przypadku 4) innego parametru , count, który określa liczbę bitów do zainicjowania. Ma dwa parametry opcjonalne i _One, które wskazują, _Zero jaki str znak powinien być interpretowany, aby oznaczać odpowiednio 0 bit i 1 bit.

6) Tworzy obiekt klasy bitset<N>, inicjując n bity wartości, które odpowiadają znakom podanym w ciągu znaków w stylu C zer i tych. Konstruktor jest wywoływany bez rzutu ciągu na typ ciągu, na przykład: bitset<5> b5("01011");

Przykład

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

int main( )
{
   // Using the default constructor
   using namespace std;
   bitset<2> b0;
   cout << "The set of bits in bitset<2> b0 is: ( "
        << b0 << " )." << endl;

   // Using the second member function
   bitset<5> b1 ( 6 );
   cout << "The set of bits in bitset<5> b1( 6 ) is: ( "
        << b1 << " )." << endl;

   // The template parameter N can be an expression
   bitset< 2 * sizeof ( int ) > b2;
   cout << "The set of bits in bitset< 2 * sizeof ( int ) > b2 is: ( "
        << b2 << " )." << endl;

   // The base two representation will be truncated
   // if its length exceeds the size of the bitset
   bitset<3> b3 ( 6 );
   cout << "The set of bits in bitset<3> b3( 6 ) is ( "
        << b3 << " )." << endl;

   // Using a c-style string to initialize the bitset
    bitset<7> b3andahalf ( "1001001" );
    cout << "The set of bits in bitset<7> b3andahalf ( \"1001001\" )"
         << " is ( " << b3andahalf << " )." << endl;

   // Using the fifth member function with the first parameter
   string bitval4 ( "10011" );
   bitset<5> b4 ( bitval4 );
   cout << "The set of bits in bitset<5> b4( bitval4 ) is ( "
        << b4 << " )." << endl;

   // Only part of the string may be used for initialization

   // Starting at position 3 for a length of 6 (100110)
   string bitval5 ("11110011011");
   bitset<6> b5 ( bitval5, 3, 6 );
   cout << "The set of bits in bitset<11> b5( bitval, 3, 6 ) is ( "
        << b5 << " )." << endl;

   // The bits not initialized with part of the string
   // will default to zero
   bitset<11> b6 ( bitval5, 3, 5 );
   cout << "The set of bits in bitset<11> b6( bitval5, 3, 5 ) is ( "
        << b6 << " )." << endl;

   // Starting at position 2 and continue to the end of the string
   bitset<9> b7 ( bitval5, 2 );
   cout << "The set of bits in bitset<9> b7( bitval, 2 ) is ( "
        << b7 << " )." << endl;
}
The set of bits in bitset<2> b0 is: ( 00 ).
The set of bits in bitset<5> b1( 6 ) is: ( 00110 ).
The set of bits in bitset<2 * sizeof ( int ) > b2 is: ( 00000000 ).
The set of bits in bitset<3> b3( 6 ) is ( 110 ).
The set of bits in bitset<5> b4( bitval4 ) is ( 10011 ).
The set of bits in bitset<11> b5( bitval, 3, 6 ) is ( 100110 ).
The set of bits in bitset<11> b6( bitval5, 3, 5 ) is ( 00000010011 ).
The set of bits in bitset<9> b7( bitval, 2 ) is ( 110011011 ).

count

Zwraca liczbę bitów ustawionych w sekwencji bitów.

size_t count() const;

Wartość zwracana

Liczba bitów ustawionych w sekwencji bitów.

Przykład

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

int main( )
{
    using namespace std;

    bitset<5> b1(4);

    cout << "The collection of bits in the original bitset is: ( "
         << b1 << " )" << endl;

    size_t i;
    i = b1.count();
    cout << "The number of bits in the bitset set to 1 is: "
         << i << "." << endl;

    bitset<5> fb1;
    fb1 = b1.flip();

    cout << "The collection of flipped bits in the modified bitset "
         << "is: ( " << b1 << " )" << endl;

    size_t ii;
    ii = fb1.count();
    cout << "The number of bits in the bitset set to 1 is: "
         << ii << "." << endl;
}
The collection of bits in the original bitset is: ( 00100 )
The number of bits in the bitset set to 1 is: 1.
The collection of flipped bits in the modified bitset is: ( 11011 )
The number of bits in the bitset set to 1 is: 4.

element_type

Typ, który jest synonimem typu bool danych i może służyć do odwołowania się do bitów elementów w obiekcie bitset.

typedef bool element_type;

Przykład

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

int main( )
{
   using namespace std;

   bitset<3> b1 ( 2 );
   cout << "Original bitset b1(6) is: ( "<< b1 << " )"
        << endl;

   //Compare two ways to reference bits in a bitset
   bool b;
   bitset<5>::element_type e;

   b = b1.test ( 2 );
   if ( b )
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 1." << endl;
   else
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 0." << endl;
   b1[2] = 1;
   cout << "Bitset b1 modified by b1[2] = 1 is: ( "<< b1 << " )"
        << endl;

   e = b1.test ( 2 );
   if ( e )
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 1." << endl;
   else
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 0." << endl;
}
Original bitset b1(6) is: ( 010 )
The bit at position 2 of bitset b1has a value of 0.
Bitset b1 modified by b1[2] = 1 is: ( 110 )
The bit at position 2 of bitset b1has a value of 1.

flip

Odwraca wartość wszystkich bitów w obiekcie bitset lub odwraca jeden bit w określonej pozycji.

bitset<N>& flip();
bitset<N>& flip(size_t pos);

Parametry

pos
Położenie bitu, którego wartość ma zostać odwrócona.

Wartość zwracana

Kopia zmodyfikowanej bitset funkcji, dla której została wywołana funkcja składowa.

Uwagi

Druga funkcja składowa zgłasza wyjątek, out_of_range jeśli pozycja określona jako parametr jest większa niż rozmiar Nbitset<N> , którego bit został odwrócony.

Przykład

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 6 );

   cout << "The collection of bits in the original bitset is: ( "
        << b1 << " )" << endl;

   bitset<5> fb1;
   fb1 = b1.flip ( );

   cout << "After flipping all the bits, the bitset becomes: ( "
        << fb1 << " )" << endl;

   bitset<5> f3b1;
   f3b1 = b1.flip ( 3 );

   cout << "After flipping the fourth bit, the bitset becomes: ( "
        << f3b1 << " )" << endl << endl;

   bitset<5> b2;
   int i;
   for ( i = 0 ; i <= 4 ; i++ )
   {
      b2.flip(i);
      cout << b2 << "  The bit flipped is in position "
           << i << ".\n";
   }
}
The collection of bits in the original bitset is: ( 00110 )
After flipping all the bits, the bitset becomes: ( 11001 )
After flipping the fourth bit, the bitset becomes: ( 10001 )

00001  The bit flipped is in position 0.
00011  The bit flipped is in position 1.
00111  The bit flipped is in position 2.
01111  The bit flipped is in position 3.
11111  The bit flipped is in position 4.

hash

template <class T> struct hash;
template <size_t N> struct hash<bitset<N>>;

Brak

Sprawdza, czy bit nie został ustawiony na 1 w bitset obiekcie.

bool none() const;

Wartość zwracana

true jeśli parametr bitu bitset nie został ustawiony na 1; false jeśli co najmniej jeden bit został ustawiony na 1.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   bool b, rb;

   cout << "Original bitset b1(6) is: ( " << b1 << " )"
        << endl;

   b = b1.none ( );

   if ( b )
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
   else
      cout << "At least one of the bits in bitset b1 is set to 1."
           << endl;

   bitset<5> rb1;
   rb1 = b1.reset ( );
   rb = rb1.none ( );
   if ( rb )
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
   else
      cout << "At least one of the bits in bitset b1 is set to 1."
           << endl;
}
Original bitset b1(6) is: ( 00110 )
At least one of the bits in bitset b1 is set to 1.
None of the bits in bitset b1 are set to 1.

operator!=

Testuje docelowy zestaw bitów pod kątem nierówności z określonym zestawem bitów.

bool operator!=(const bitset<N>& right) const;

Parametry

right
Wartość bitset ta ma być porównywana z docelowym zestawem bitów pod kątem nierówności.

Wartość zwracana

true jeśli zestawy bitów są inne; false jeśli są takie same.

Uwagi

Zestawy bitów muszą mieć taki sam rozmiar.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 7 );
   bitset<5> b3 ( 2 );
   bitset<4> b4 ( 7 );

   if ( b1 != b2 )
      cout << "Bitset b1 is different from bitset b2." << endl;
   else
      cout << "Bitset b1 is the same as bitset b2." << endl;

   if ( b1 != b3 )
      cout << "Bitset b1 is different from bitset b3." << endl;
   else
      cout << "Bitset b1 is the same as bitset b3." << endl;

   // This would cause an error because bitsets must have the
   // same size to be tested
   // if ( b1 != b4 )
   //   cout << "Bitset b1 is different from bitset b4." << endl;
   // else
   //   cout << "Bitset b1 is the same as bitset b4." << endl;
}
Bitset b1 is the same as bitset b2.
Bitset b1 is different from bitset b3.

operator&=

Wykonuje bitową kombinację zestawów bitów z bitową operacją "and" (&).

bitset<N>& operator&=(const bitset<N>& right);

Parametry

right
Element bitset , który ma być połączony bitowo z docelowym zestawem bitów.

Wartość zwracana

Zmodyfikowany docelowy zestaw bitów, który wynika z bitowej operacji "i" (&) z bitset określonym jako parametr.

Uwagi

Dwa bity połączone przez AND operator zwracają true , jeśli każdy bit ma wartość true; w przeciwnym razie ich kombinacja zwraca wartość false.

Dwa zestawy bitów muszą mieć ten sam rozmiar.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 &= b2;
   cout << "After bitwise AND combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset is unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bisets
   // must be of the same size to be combined
   // b1 &= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise AND combination,
the target bitset b1 becomes:   ( 00011 ).
The parameter bitset b2 remains: ( 01011 ).

operator<<

Przesuwa bity w kierunku bitset po lewej stronie określoną liczbę pozycji i zwraca wynik na nową bitsetwartość .

bitset<N> operator<<(size_t pos) const;

Parametry

pos
Liczba pozycji po lewej stronie, które bity w obiekcie bitset mają być przesunięte.

Wartość zwracana

Zmodyfikowany zestaw bitów z bitami przesunięty w lewo o wymaganą liczbę pozycji.

Uwagi

Funkcja operatora elementu członkowskiego zwraca bitset(*this) <<= pos , gdzie <<= przesuwa bity w lewo bitset określoną liczbę pozycji i zwraca wynik do docelowej bitset.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The bitset b1 is: ( "<< b1 << " )." << endl;

   bitset<5> b2;
   b2 = b1 << 2;

   cout << "After shifting the bits 2 positions to the left,\n"
        << " the bitset b2 is: ( "<< b2 << " )."
        << endl;

   bitset<5> b3 = b2 >> 1;

   cout << "After shifting the bits 1 position to the right,\n"
        << " the bitset b3 is: ( " << b3 << " )."
        << endl;
}

operator<<=

Przesuwa bity w lewo bitset określoną liczbę pozycji i zwraca wynik do docelowej bitset.

bitset<N>& operator<<=(size_t pos);

Parametry

pos
Liczba pozycji po lewej stronie bitów w obiekcie bitset ma zostać przesunięta.

Wartość zwracana

Docelowa bitset modyfikacja została zmieniona tak, aby bity zostały przesunięte w lewo z wymaganą liczbą pozycji.

Uwagi

Jeśli żaden element nie istnieje, aby przesunąć się do pozycji, funkcja wyczyści bit do wartości 0.

Przykład

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   cout << "The target bitset b1 is: ( "<< b1 << " )." << endl;
   b1 <<= 2;
   cout << "After shifting the bits 2 positions to the left,\n"
        << "the target bitset b1 becomes: ( "<< b1 << " )."
        << endl;
}
The target bitset b1 is: ( 00111 ).
After shifting the bits 2 positions to the left,
the target bitset b1 becomes: ( 11100 ).

operator==

Testuje docelowy zestaw bitów pod kątem równości z określonym zestawem bitów.

bool operator==(const bitset<N>& right) const;

Parametry

right
Wartość bitset ta ma być porównywana z docelowym zestawem bitów pod kątem równości.

Wartość zwracana

true jeśli zestawy bitów są takie same; false jeśli są różne.

Uwagi

Zestawy bitów muszą mieć taki sam rozmiar.

Przykład

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 7 );
   bitset<5> b3 ( 2 );
   bitset<4> b4 ( 7 );

   if ( b1 == b2 )
      cout << "Bitset b1 is the same as bitset b2." << endl;
   else
      cout << "Bitset b1 is different from bitset b2." << endl;

   if ( b1 == b3 )
      cout << "Bitset b1 is the same as bitset b3." << endl;
   else
      cout << "Bitset b1 is different from bitset b3." << endl;

   // This would cause an error because bitsets must have the
   // same size to be tested
   // if ( b1 == b4 )
   //   cout << "Bitset b1 is the same as bitset b4." << endl;
   // else
   //   cout << "Bitset b1 is different from bitset b4." << endl;
}
Bitset b1 is the same as bitset b2.
Bitset b1 is different from bitset b3.

operator>>

Przenosi bity w prawo bitset do określonej liczby pozycji i zwraca wynik do nowego zestawu bitów.

bitset<N> operator>>(size_t pos) const;

Parametry

pos
Liczba pozycji po prawej stronie bitów w obiekcie bitset ma zostać przesunięta.

Wartość zwracana

Nowy zestaw bitów, w którym bity zostały przesunięte w prawo do wymaganej liczby pozycji względem docelowej bitset.

Przykład

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   cout << "The bitset b1 is: ( "<< b1 << " )." << endl;

   bitset<5> b2;
   b2 = b1 << 2;

   cout << "After shifting the bits 2 positions to the left,\n"
        << "the bitset b2 is: ( "<< b2 << " )."
        << endl;
   bitset<5> b3 = b2 >> 1;

   cout << "After shifting the bits 1 position to the right,\n"
        << "the bitset b3 is: ( " << b3 << " )."
        << endl;
}
The bitset b1 is: ( 00111 ).
After shifting the bits 2 positions to the left,
the bitset b2 is: ( 11100 ).
After shifting the bits 1 position to the right,
the bitset b3 is: ( 01110 ).

operator>>=

Przenosi bity w prawo bitset do określonej liczby pozycji i zwraca wynik do docelowej bitset.

bitset<N>& operator>>=(size_t pos);

Parametry

pos
Liczba pozycji po prawej stronie bitów w obiekcie bitset ma zostać przesunięta.

Wartość zwracana

Docelowa bitset modyfikacja tak, aby bity zostały przesunięte w prawo do wymaganej liczby pozycji.

Uwagi

Jeśli żaden element nie istnieje, aby przesunąć się do pozycji, funkcja wyczyści bit do wartości 0.

Przykład

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 28 );
   cout << "The target bitset b1 is: ( "<< b1 << " )." << endl;

   b1 >>= 2;
   cout << "After shifting the bits 2 positions to the right,\n"
        << "the target bitset b1 becomes: ( "<< b1 << " )."
        << endl;
}
The target bitset b1 is: ( 11100 ).
After shifting the bits 2 positions to the right,
the target bitset b1 becomes: ( 00111 ).

operator[]

Zwraca odwołanie do bitu na określonej pozycji w bitset obiekcie , jeśli bitset jest modyfikowalne. W przeciwnym razie zwraca wartość bitu w tej pozycji.

bool operator[](size_t pos) const;
reference operator[](size_t pos);

Parametry

pos
Położenie lokalizowania bitu w obiekcie bitset.

Uwagi

Podczas definiowania _ITERATOR_DEBUG_LEVEL wartości 1 lub 2 w kompilacji w pliku wykonywalnym wystąpi błąd środowiska uruchomieniowego, jeśli próbujesz uzyskać dostęp do elementu poza granicami bitset. Aby uzyskać więcej informacji, zobacz Sprawdzono iteratory.

Przykład

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

int main( )
{
   using namespace std;
   bool b;
   bitset<5> b1 ( 6 );
   cout << "The initialized bitset<5> b1( 2 ) is: ( "<< b1 << " )."
        << endl;

   int i;
   for ( i = 0 ; i <= 4 ; i++ )
   {
      b = b1[ i ];
      cout << "  The bit in position "
           << i << " is " << b << ".\n";
   }
}

operator^=

Wykonuje bitową kombinację zestawów bitów z bitową operacją "xor" (^).

bitset<N>& operator^=(const bitset<N>& right);

Parametry

right
Element bitset , który ma być połączony bitowo z docelowym zestawem bitów.

Wartość zwracana

Zmodyfikowany docelowy zestaw bitów, który wynika z bitowej operacji "xor" (^) z bitset określonym jako parametr.

Uwagi

Dwa bity połączone przez operator bitowy "xor" () zwracajątrue, jeśli co najmniej jeden, ale nie oba bity są true; w przeciwnym razie ich kombinacja zwraca wartość false.^

Zestawy bitów muszą mieć taki sam rozmiar.

Przykład

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 ^= b2;
   cout << "After bitwise exclusive OR combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset in unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bitsets
   // must be of the same size to be combined
   // b1 |= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise exclusive OR combination,
the target bitset b1 becomes:   ( 01100 ).
The parameter bitset b2 remains: ( 01011 ).

operator|=

Łączy dwa zestawy bitów przy użyciu bitowej operacji "or" (|).

bitset<N>& operator|=(const bitset<N>& right);

Parametry

right
Element bitset , który ma być połączony bitowo z docelowym bitsetelementem .

Wartość zwracana

Zmodyfikowany docelowy zestaw bitów, który wynika z bitowej operacji "or" (|) z bitset określonym jako parametr.

Uwagi

Dwa bity połączone przez operator inkluzywny OR zwracają true , jeśli co najmniej jeden z bitów jest true; jeśli oba bity są false, ich kombinacja zwraca wartość false.

Zestawy bitów muszą mieć taki sam rozmiar.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 |= b2;
   cout << "After bitwise inclusive OR combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset in unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bisets
   // must be of the same size to be combined
   // b1 |= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise inclusive OR combination,
the target bitset b1 becomes:   ( 01111 ).
The parameter bitset b2 remains: ( 01011 ).

operator~

Odwraca wszystkie bity w docelowym zestawie bitów i zwraca wynik.

bitset<N> operator~() const;

Wartość zwracana

Element bitset ze wszystkimi jego bitami odwrócony w odniesieniu do docelowego bitset.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2;
   b2 = ~b1;

   cout << "Bitset b1 is: ( "<< b1 << " )." << endl;
   cout << "Bitset b2 = ~b1 is: ( "<< b2 << " )." << endl;

   // These bits could also be flipped using the flip member function
   bitset<5> b3;
   b3 = b1.flip( );
   cout << "Bitset b3 = b1.flip( ) is: ( "<< b2 << " )." << endl;
}
Bitset b1 is: ( 00111 ).
Bitset b2 = ~b1 is: ( 11000 ).
Bitset b3 = b1.flip( ) is: ( 11000 ).

reference

Klasa serwera proxy, która zawiera odwołania do bitów zawartych w obiekcie bitset używanym do uzyskiwania dostępu do poszczególnych bitów i manipulowania nimi jako klasy pomocniczej klasy 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();
};

Parametry

val
Wartość obiektu typu bool , który ma być przypisany do bitu w obiekcie bitset.

bitref
Odwołanie do formularza x [ i ] do bitu na pozycji w .ibitsetx

Wartość zwracana

Odwołanie do bitu określonego bitset przez pozycję argumentu dla pierwszych, drugich i piątych funkcji składowych odwołań klasy oraz true , falseaby odzwierciedlać wartość zmodyfikowanego bitu w bitset funkcji trzeciej i czwartej składowej odwołania klasy.

Uwagi

Klasa reference istnieje tylko jako klasa pomocnika dla klasy bitsetoperator[]. Klasa składowa opisuje obiekt, który może uzyskać dostęp do pojedynczego bitu bitsetw obiekcie . Niech b będzie obiektem typu bool, x i y obiektów typu bitset<N>i prawidłowych ij pozycji w obrębie takiego obiektu. Notacja x [i] odwołuje się do bitu na pozycji i w zestawie bitów x. Funkcje składowe klasy reference zapewniają w kolejności następujące operacje:

Operacja Definicja
x[i] = b bool Przechowuje wartość b na pozycji i bitowej w zestawie bitów x.
x[i] = y[j] Przechowuje wartość bitu y[ j] na pozycji i bitowej w zestawie bitów x.
b = ~ x[i] Przechowuje przerzucaną wartość bitu x[ i] w .boolb
b = x[i] Przechowuje wartość bitu x[ i] w .boolb
x[i]. flip( ) Przechowuje przerzucaną wartość bitu x[ i] z powrotem na pozycji bitowej w .ix

Przykład

// 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\nof 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,\nit is ( "<<  b2  << " )." << endl;
   b2 [4].flip( );
   cout << "After flipping the value of the bit at position 4 in "
        << "bitset b2,\nit becomes ( "<<  b2  << " )." << endl;
   bool c;
   c = b2 [4].flip( );
   cout << "After a second flip, the value of the position 4 "
        << "bit in b2 is now: " << c << ".";
}
The initialized bitset<5> b1( 2 ) is: ( 00010 ).
The initialized bitset<5> b2( 6 ) is: ( 00110 ).
The bitset<5> b1 with the bit at position 0 set to 1 is: ( 00011 )
The bitset<5> b2 with the bit at position 4 set to the value
of the bit at position 0 of the bit in bitset<5> b1 is: ( 10110 )
The value of the object b = ~b2 [4] of type bool is false.
The value of the object b = b2 [4] of type bool is true.
Before flipping the value of the bit at position 4 in bitset b2,
it is ( 10110 ).
After flipping the value of the bit at position 4 in bitset b2,
it becomes ( 00110 ).
After a second flip, the value of the position 4 bit in b2 is now: 1.

reset

Resetuje wszystkie bity w bitset 0 lub resetuje bit w określonej pozycji do 0.

bitset<N>& reset();
bitset<N>& reset(size_t pos);

Parametry

pos
Pozycja bitu w bitset obiekcie , który ma zostać zresetowany do wartości 0.

Wartość zwracana

Kopia bitset funkcji składowej, dla której została wywołana.

Uwagi

Druga funkcja składowa zgłasza out_of_range wyjątek, jeśli określona pozycja jest większa niż rozmiar elementu bitset.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 13 );
   cout << "The set of bits in bitset<5> b1(13) is: ( "<< b1 << " )"
        << endl;

   bitset<5> b1r3;
   b1r3 = b1.reset( 2 );
   cout << "The collection of bits obtained from resetting the\n"
        << "third bit of bitset b1 is: ( "<< b1r3 << " )"
        << endl;

   bitset<5> b1r;
   b1r = b1.reset( );
   cout << "The collecion of bits obtained from resetting all\n"
        << "the elements of the bitset b1 is: ( "<< b1r << " )"
        << endl;
}
The set of bits in bitset<5> b1(13) is: ( 01101 )
The collecion of bits obtained from resetting the
third bit of bitset b1 is: ( 01001 )
The collecion of bits obtained from resetting all
the elements of the bitset b1 is: ( 00000 )

set

Ustawia wszystkie bity na bitset wartość 1 lub ustawia bit w określonej pozycji na 1.

bitset<N>& set();

bitset<N>& set(
    size_t pos,
    bool val = true);

Parametry

pos
Położenie bitu w bitset obiekcie , aby ustawić przypisaną wartość.

val
Wartość, która ma zostać przypisana do bitu w określonej pozycji.

Wartość zwracana

Kopia bitset funkcji składowej, dla której została wywołana.

Uwagi

Druga funkcja składowa zgłasza out_of_range wyjątek, jeśli określona pozycja jest większa niż rozmiar elementu bitset.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   cout << "The set of bits in bitset<5> b1(6) is: ( "<< b1 << " )"
        << endl;

   bitset<5> b1s0;
   b1s0 = b1.set( 0 );
   cout << "The collecion of bits obtained from setting the\n"
        << "zeroth bit of bitset b1 is: ( "<< b1s0 << " )"
        << endl;

   bitset<5> bs1;
   bs1 = b1.set( );
   cout << "The collecion of bits obtained from setting all the\n"
        << "elements of the bitset b1 is: ( "<< bs1 << " )"
        << endl;
}
The set of bits in bitset<5> b1(6) is: ( 00110 )
The collecion of bits obtained from setting the
zeroth bit of bitset b1 is: ( 00111 )
The collecion of bits obtained from setting all the
elements of the bitset b1 is: ( 11111 )

size

Zwraca liczbę bitów w bitset obiekcie.

size_t size() const;

Wartość zwracana

Liczba bitów, N, w .bitset<N>

Przykład

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

int main()
{
    using namespace std;

    bitset<5> b1(6);
    size_t i;

    cout << "The set of bits in bitset<5> b1( 6 ) is: ( "<< b1 << " )"
         << endl;

    i = b1.size();

    cout << "The number of bits in bitset b1 is: " << i << "."
         << endl;
}
The set of bits in bitset<5> b1( 6 ) is: ( 00110 )
The number of bits in bitset b1 is: 5.

test

Sprawdza, czy bit na określonej pozycji w bitset obiekcie ma wartość 1.

bool test(size_t pos) const;

Parametry

pos
Pozycja bitu w obiekcie do przetestowania pod kątem bitset jego wartości.

Wartość zwracana

true jeśli bit określony przez pozycję argumentu jest ustawiony na 1; w przeciwnym razie, false.

Uwagi

Funkcja składowa zgłasza błąd out_of_range

to_string

Konwertuje bitset obiekt na reprezentację ciągu.

template <class charT = char, class traits = char_traits<charT>, class Allocator = allocator<charT> >
   basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;

Wartość zwracana

Obiekt ciągu klasy basic_string, gdzie każdy bit ustawiony w obiekcie bitset ma odpowiedni znak 1 i znak 0, jeśli bit nie jest ustawiony.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The ordered set of bits in the bitset<5> b1( 7 )"
        << "\n  that was generated by the number 7 is: ( "
        << b1 << " )" << endl;

   string s1;
   s1 =  b1.template to_string<char,
   char_traits<char>, allocator<char> >( );
   cout << "The string returned from the bitset b1"
        << "\n  by the member function to_string( ) is: "
        << s1 << "." << endl;
}
The ordered set of bits in the bitset<5> b1( 7 )
  that was generated by the number 7 is: ( 00111 )
The string returned from the bitset b1
  by the member function to_string( ) is: 00111.

to_ullong

Zwraca wartość zawierającą unsigned long long te same bity ustawione jako zawartość bitset obiektu.

unsigned long long to_ullong() const;

Wartość zwracana

Zwraca sumę wartości bitowych, które znajdują się w sekwencji bitów jako unsigned long long. Ta unsigned long long wartość spowoduje ponowne utworzenie tych samych bitów zestawu, jeśli zostanie użyta do zainicjowania elementu bitset.

Wyjątki

Zgłasza obiekt, overflow_error jeśli jakikolwiek bit w sekwencji bitów ma wartość bitową, która nie może być reprezentowana jako wartość typu unsigned long long.

Uwagi

Zwraca sumę wartości bitowych, które znajdują się w sekwencji bitów jako unsigned long long.

to_ulong

Konwertuje bitset obiekt na liczbę całkowitą, która wygeneruje sekwencję bitów zawartych, jeśli zostanie użyta do zainicjowania obiektu bitset.

unsigned long to_ulong( ) const;

Wartość zwracana

Liczba całkowita, która wygeneruje bity w obiekcie , bitset jeśli zostanie użyta podczas inicjowania bitsetelementu .

Uwagi

Zastosowanie funkcji składowej spowoduje zwrócenie liczby całkowitej, która ma tę samą sekwencję 1 i 0 cyfr, co znajduje się w sekwencji bitów zawartych w elemencie bitset.

Funkcja składowa zgłasza overflow_error obiekt, jeśli jakikolwiek bit w sekwencji bitowej ma wartość bitową, która nie może być reprezentowana jako wartość typu unsigned long.

Przykład

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The ordered set of bits in the bitset<5> b1( 7 )"
        << "\n  that was generated by the number 7 is: ( "
        << b1 << " )" << endl;

   unsigned long int i;
   i = b1.to_ulong( );
   cout << "The integer returned from the bitset b1,"
        << "\n  by the member function to_long( ), that"
        << "\n  generated the bits as a base two number is: "
        << i << "." << endl;
}
The ordered set of bits in the bitset<5> b1( 7 )
  that was generated by the number 7 is: ( 00111 )
The integer returned from the bitset b1,
  by the member function to_long( ), that
  generated the bits as a base two number is: 7.