Třída bitset

Popisuje typ objektu, který ukládá posloupnost skládající se z pevného počtu bitů, které poskytují kompaktní způsob uchovávání příznaků pro sadu položek nebo podmínek. Třída bitset podporuje operace s objekty typu bitset, které obsahují kolekci bitů a poskytují konstantní přístup k jednotlivým bitům.

Syntaxe

template <size_t N>
class bitset

Parametry

N
Určuje počet bitů v objektu bitset s nenulovým celé číslo typu size_t , které musí být známo v době kompilace.

Poznámky

Na rozdíl od podobné vector<bool> třídybitset třída nemá iterátory a není kontejnerem standardní knihovny C++. Liší se také od vector<bool> určité konkrétní velikosti, která je pevná v době kompilace v souladu s velikostí určenou parametrem N šablony při bitset<N> deklaraci.

Bit se nastaví, pokud je jeho hodnota 1 a resetuje se, pokud je její hodnota 0. Chcete-li překlopit nebo invertovat bit, je změnit jeho hodnotu z 1 na 0 nebo od 0 do 1. Bity N v objektu bitset jsou indexovány celočíselnou hodnotou od 0 do N - 1, kde 0 indexuje pozici prvního bitu a N - 1 poslední bitovou pozici.

Členové

Konstruktory

Jméno popis
bitset Vytvoří objekt třídy bitset<N> a inicializuje bity na nulu, na určitou hodnotu nebo na hodnoty získané z znaků v řetězci.

Typedefs

Jméno popis
element_type Typ, který je synonymem pro datový typ bool a lze jej použít k odkazování na bity prvků v objektu bitset.

Funkce

Jméno popis
all Otestuje všechny bity v tomto bitset případě a určí, jestli jsou všechny nastavené na true.
any Členová funkce testuje, zda je libovolný bit v sekvenci nastaven na hodnotu 1.
count Členová funkce vrátí počet bitů nastavených v bitové sekvenci.
flip Invertuje hodnotu všech bitů v bitset zadané pozici nebo invertuje jeden bit.
none Testuje, zda nebyl v objektu bitset nastaven žádný bit na hodnotu 1.
reset Obnoví všechny bity v bitset 0 nebo obnoví bit v zadané pozici na 0.
set Nastaví všechny bity v bitset 1 nebo nastaví bit na zadanou pozici na 1.
size Vrátí počet bitů v objektu bitset .
test Testuje, zda je bit na zadané pozici v bitset bodě nastaven na hodnotu 1.
to_string bitset Převede objekt na řetězcovou reprezentaci.
to_ullong Vrátí součet bitových hodnot v bitové hodnotě bitsetunsigned long longjako .
to_ulong bitset Převede objekt na unsigned long ten, který by vygeneroval sekvenci bitů obsažených, pokud se používá k inicializaci bitset.

Třídy

Jméno popis
reference Proxy třída, která poskytuje odkazy na bity obsažené v objektu bitset , který slouží k přístupu k jednotlivým bitům a manipulaci s nimi jako pomocné třídy třídy operator[]bitset.

Operátory

Jméno popis
operator!= Testuje cíl bitset nerovnosti se zadaným bitsetparametrem .
operator&= Používá bitovou kombinaci bitových sad s bitové operace "and" (&).
operator<< Posune bity doleva bitset zadaný počet pozic a vrátí výsledek na nový bitset.
operator<<= Posune bity doleva bitset zadaný počet pozic a vrátí výsledek cílovému bitset.
operator== Testuje cíl bitset rovnosti se zadaným bitsetparametrem .
operator>> Posune bity na bitset pravé straně zadaný počet pozic a vrátí výsledek na nový bitset.
operator>>= Posune bity v bitset rohu doprava zadaný počet pozic a vrátí výsledek na cíl bitset.
operator[] Vrátí odkaz na bit na zadané pozici v bitset případě, že bitset je upravitelný. V opačném případě vrátí hodnotu bitu v dané pozici.
operator^= Používá bitovou kombinaci bitových sad s bitové operace "xor" (^).
operator|= Používá bitovou kombinaci bitových sad s bitovou operací "or" (|neboli).
operator~ Invertuje všechny bity v cíli bitset a vrátí výsledek.

Struktury

Jméno popis
hash

all

Otestuje všechny bity v této bitové sadě a určí, jestli jsou všechny nastavené na true.

bool all() const;

Vrácená hodnota

Vrátí true , pokud jsou všechny bity v této sadě pravdivé. Vrátí false hodnotu, pokud je jeden nebo více bitů false.

any

Testuje, zda je libovolný bit v sekvenci nastaven na hodnotu 1.

bool any() const;

Vrácená hodnota

true pokud je v tomto bitset bitu nastavená hodnota 1, false pokud jsou všechny bity 0.

Příklad

// 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

Vytvoří objekt třídy bitset<N> a inicializuje bity na nulu nebo na určitou zadanou hodnotu nebo na hodnoty získané z znaků v řetězci.

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
Celé číslo bez znaménka, jehož základní dvě reprezentace se používá k inicializaci bitů ve vytvořeném objektu bitset .

str
Řetězec nul a těch použitých k inicializaci bitových bitset hodnot.

pos
Pozice znaku v řetězci, počítá se zleva doprava a začíná nulou, která slouží k inicializaci prvního bitu v objektu bitset.

count
Početznakůch bitset

Zero
Znak, který se používá k reprezentaci nuly. Výchozí hodnota je 0.

One
Znak, který se používá k reprezentaci jednoho. Výchozí hodnota je 1.

Poznámky

1) Vytvoří objekt třídy bitset<N> a inicializuje všechny N bity na výchozí hodnotu nuly.

2-3) Vytvoří objekt třídy bitset<N> a inicializuje bity z parametru val .

4) Vytvoří objekt třídy bitset<N> a inicializuje bity ze znaků zadaných v řetězci nuly a jedničky. Pokud jsou některé znaky řetězce jiné než 0 nebo 1, konstruktor vyvolá objekt třídy invalid argument. Pokud zadaná pozice (pos) přesahuje délku řetězce, pak konstruktor vyvolá objekt třídy out_of_range. Konstruktor nastaví pouze ty bity na pozici j , bitset pro které je znak v řetězci na pozici pos + j 1. Ve výchozím nastavení pos je 0.

5) Podobá se 4) ale i dalšímu parametru, countkterý určuje počet bitů, které se mají inicializovat. Má dva volitelné parametry, _Zero které _Oneoznačují, v str jakém znaku by se měl interpretovat, aby znamenal 0 bit a 1 bit.

6) Vytvoří objekt třídy bitset<N>, inicializace N bitů na hodnoty, které odpovídají znakům zadaným v řetězci znaků ve stylu jazyka C nuly a jedničky. Konstruktor zavoláte bez přetypování řetězce na typ řetězce, například: bitset<5> b5("01011");

Příklad

// 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

Vrátí početbitch

size_t count() const;

Vrácená hodnota

Počet bitů nastavených v bitové sekvenci

Příklad

// 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, který je synonymem pro datový typ bool a lze jej použít k odkazování na bity prvků v objektu bitset.

typedef bool element_type;

Příklad

// 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

Invertuje hodnotu všech bitů v bitset zadané pozici nebo invertuje jeden bit.

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

Parametry

pos
Pozice bitu, jehož hodnota má být invertována.

Vrácená hodnota

Kopie změněné bitset funkce, pro kterou byla vyvolána členová funkce.

Poznámky

Druhá členová funkce vyvolá out_of_range výjimku, pokud je pozice zadaná jako parametr větší než velikost N bitu bitset<N> , jehož bit byl invertován.

Příklad

// 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>>;

žádný

Testuje, zda nebyl v objektu bitset nastaven žádný bit na hodnotu 1.

bool none() const;

Vrácená hodnota

true pokud nebyl v bitu bitset nastaven na hodnotu 1; false pokud byl alespoň jeden bit nastaven na hodnotu 1.

Příklad

// 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!=

Otestuje cílovou bitovou sadu pro nerovnost se zadanou bitovou sadou.

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

Parametry

right
To bitset je porovnat s cílovou bitovou sadou pro nerovnost.

Vrácená hodnota

true jsou-li bitové sady odlišné; false pokud jsou stejné.

Poznámky

Bitsety musí mít stejnou velikost.

Příklad

// 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&=

Používá bitovou kombinaci bitových sad s bitové operace "and" (&).

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

Parametry

right
To bitset je bitové spojení s cílovou bitovou sadou.

Vrácená hodnota

Upravená cílová bitová sada, která je výsledkem bitové operace "and" (&) se bitset zadaným parametrem.

Poznámky

Dva bity kombinované operátorem AND vrátí true , pokud je každý bit pravdivý; jinak jejich kombinace vrátí false.

Dvě sady bitů musí mít stejnou velikost.

Příklad

// 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<<

Posune bity doleva bitset zadaný počet pozic a vrátí výsledek na nový bitset.

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

Parametry

pos
Počet pozic vlevo, které mají být bity v tabulce bitset posunuty.

Vrácená hodnota

Upravená bitová sada s bity posunutými vlevo požadovaný počet pozic.

Poznámky

Funkce operátoru člena vrátí bitset(*this) <<= pos , kde <<= posune bity doleva bitset zadaný počet pozic a vrátí výsledek cílovému bitset.

Příklad

// 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<<=

Posune bity doleva bitset zadaný počet pozic a vrátí výsledek cílovému bitset.

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

Parametry

pos
Počet pozic vlevo od bitů, které mají být posunuty bitset .

Vrácená hodnota

Cílená bitset změna tak, aby bity byly posunuty vlevo požadovaný počet pozic.

Poznámky

Pokud neexistuje žádný prvek pro posun do pozice, funkce vymaže bit na hodnotu 0.

Příklad

// 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==

Otestuje cílovou bitovou sadu pro rovnost se zadanou bitovou sadou.

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

Parametry

right
To bitset se má porovnat s cílovou bitovou sadou pro rovnost.

Vrácená hodnota

true pokud jsou bitsety stejné; false pokud jsou jiné.

Poznámky

Bitsety musí mít stejnou velikost.

Příklad

// 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>>

Posune bity na bitset pravé straně zadaný počet pozic a vrátí výsledek na novou bitovou sadu.

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

Parametry

pos
Počet pozic napravo, které mají být bity v tabulce bitset posunuty.

Vrácená hodnota

Nová bitová sada, ve které byly bity posunuty doprava požadovaný počet pozic vzhledem k cíli bitset.

Příklad

// 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>>=

Posune bity v bitset rohu doprava zadaný počet pozic a vrátí výsledek na cíl bitset.

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

Parametry

pos
Počet pozic napravo, které mají být bity v tabulce bitset posunuty.

Vrácená hodnota

Cílená bitset změna tak, aby byly bity posunuty doprava požadovaný počet pozic.

Poznámky

Pokud neexistuje žádný prvek pro posun do pozice, funkce vymaže bit na hodnotu 0.

Příklad

// 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[]

Vrátí odkaz na bit na zadané pozici v bitset případě, že bitset je upravitelný. V opačném případě vrátí hodnotu bitu v dané pozici.

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

Parametry

pos
Umístění umístění bitu uvnitř objektu bitset.

Poznámky

Když v sestavení definujete _ITERATOR_DEBUG_LEVEL hodnotu 1 nebo 2, dojde ve spustitelném souboru k chybě za běhu, pokud se pokusíte získat přístup k prvku mimo hranice objektu bitset. Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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^=

Používá bitovou kombinaci bitových sad s bitové operace "xor" (^).

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

Parametry

right
To bitset je bitové spojení s cílovou bitovou sadou.

Vrácená hodnota

Upravená cílová bitová sada, která je výsledkem bitové operace "xor" (^) se bitset zadaným parametrem.

Poznámky

Dva bity kombinované operátorem "xor" (^) vrátí true , pokud alespoň jeden, ale ne oba bity jsou true; jinak jejich kombinace vrátí false.

Bitsety musí mít stejnou velikost.

Příklad

// 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|=

Kombinuje dvě bitové sady pomocí bitové operace "or" (|).

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

Parametry

right
To bitset je zkombinovat bitové s cílem bitset.

Vrácená hodnota

Upravená cílová bitová sada, která je výsledkem bitové operace "or" (|) se bitset zadaným parametrem.

Poznámky

Dvě bity kombinované operátorem inkluzivního OR operátoru vrátí true , pokud je truealespoň jeden z bitů ; pokud jsou falseoba bity , jejich kombinace vrátí false.

Bitsety musí mít stejnou velikost.

Příklad

// 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~

Invertuje všechny bity v cílové bitové sadě a vrátí výsledek.

bitset<N> operator~() const;

Vrácená hodnota

S bitset veškerými jeho bity invertovány s ohledem na cíle bitset.

Příklad

// 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

Proxy třída, která poskytuje odkazy na bity obsažené v objektu bitset , který slouží k přístupu k jednotlivým bitům a manipulaci s nimi jako pomocné třídy třídy 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
Hodnota objektu typu bool , který má být přiřazen k bitu v objektu bitset.

bitref
Odkaz formuláře x [ i ] na bit na pozici i v bitsetx.

Vrácená hodnota

Odkaz na bit v bitset zadané pozici argumentu pro první, druhý a pátý člen funkce odkazu na třídu a true nebo false, aby odrážel hodnotu upraveného bitu v bitset pro třetí a čtvrté členské funkce odkazu třídy.

Poznámky

Třída reference existuje pouze jako pomocná třída pro bitsetoperator[]třídu . Člen třída popisuje objekt, který má přístup k jednotlivým bitům v rámci .bitset Let b být objekt typu bool, x a y objekty typu bitset<N>a a ij platné pozice v rámci tohoto objektu. Zápis x [i] odkazuje na bit na pozici i v bitsetu x. Členské funkce třídy reference poskytují v pořadí následující operace:

Operace Definice
x[i] = b Ukládá bool hodnotu b na pozici i bitu v bitset x.
x[i] = y[j] Ukládá hodnotu bitu y[ j] na pozici i bitu v bitset x.
b = ~ x[i] Ukládá překlopenou hodnotu bitu x[ i] v boolb.
b = x[i] Ukládá hodnotu bitu x[ i] v boolb.
x[i]. flip( ) Ukládá překlopenou hodnotu bitu x[ i] zpět na pozici i bitu 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\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

Obnoví všechny bity v bitset 0 nebo obnoví bit v zadané pozici na 0.

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

Parametry

pos
Pozice bitu bitset , který se má obnovit na 0.

Vrácená hodnota

Kopie bitset členské funkce, pro kterou byla vyvolána.

Poznámky

Druhá členová funkce vyvolá out_of_range výjimku, pokud je zadaná pozice větší než velikost bitset.

Příklad

// 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

Nastaví všechny bity v bitset 1 nebo nastaví bit na zadanou pozici na 1.

bitset<N>& set();

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

Parametry

pos
Pozice bitu v bitset přiřazené hodnotě.

val
Hodnota, která se má přiřadit k bitu na zadané pozici.

Vrácená hodnota

Kopie bitset členské funkce, pro kterou byla vyvolána.

Poznámky

Druhá členová funkce vyvolá out_of_range výjimku, pokud je zadaná pozice větší než velikost bitset.

Příklad

// 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

Vrátí počet bitů v objektu bitset .

size_t size() const;

Vrácená hodnota

Počet bitů, Nv .bitset<N>

Příklad

// 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

Testuje, zda je bit na zadané pozici v bitset bodě nastaven na hodnotu 1.

bool test(size_t pos) const;

Parametry

pos
Pozice bitu v testované hodnotě bitset .

Vrácená hodnota

truepokud je bit určený pozicí argumentu nastaven na hodnotu 1; v opačném případě . false

Poznámky

Členová funkce vyvolá výjimku out_of_range

to_string

bitset Převede objekt na řetězcovou reprezentaci.

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;

Vrácená hodnota

Řetězcový objekt třídy basic_string, kde každý bit nastaven v bitset sadě má odpovídající znak 1 a znak 0, pokud bit není nastaven.

Příklad

// 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

unsigned long long Vrátí hodnotu, která obsahuje stejné bity nastavené jako obsah objektubitset.

unsigned long long to_ullong() const;

Vrácená hodnota

Vrátí součet bitových hodnot, které jsou v bitové sekvenci jako unsigned long long. Tato unsigned long long hodnota by znovu vytvořila stejné bity sady, pokud se používá k inicializaci bitset.

Výjimky

Vyvolá objekt, overflow_error pokud jakýkoli bit v bitové sekvenci má bitovou hodnotu, která nemůže být reprezentována jako hodnota typu unsigned long long.

Poznámky

Vrátí součet bitových hodnot, které jsou v bitové sekvenci jako unsigned long long.

to_ulong

bitset Převede objekt na celé číslo, které by vygenerovalo sekvenci bitů obsažených v případě použití k inicializaci bitset.

unsigned long to_ulong( ) const;

Vrácená hodnota

Celé číslo, které by vygenerovalo bity v bitset případě použití při inicializaci bitset.

Poznámky

Použití členské funkce vrátí celé číslo, které má stejnou sekvenci 1 a 0 číslic, jak je nalezeno v sekvenci bitů obsažených v bitsetsouboru .

Členské funkce vyvolá objekt, overflow_error pokud jakýkoli bit v bitové sekvenci má bitovou hodnotu, která nemůže být reprezentována jako hodnota typu unsigned long.

Příklad

// 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.