Sdílet prostřednictvím


bitset::bitset

Vytvoří objekt třídy bitset<N> a inicializuje bitů na nulu, nebo některé zadané hodnoty nebo hodnoty získané ze znaků v řetězci.

bitset( );
bitset(
   unsigned long long _Val
);
explicit bitset(
   const char * _CStr
); 
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
  );
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’)
);

Parametry

  • _Val
    Jehož base dva zastoupení slouží k inicializaci bitů v bitset nevytváří číslo bez znaménka.

  • _Str
    Řetězec nuly a jedničky slouží k inicializaci hodnot bitů bitset.

  • _CStr
    C styl řetězec nuly a jedničky slouží k inicializaci hodnot bitů bitset.

  • _Pos
    Pozice znaku v řetězci počítání zleva doprava a počínaje nula slouží k inicializaci první bit v bitset.

  • _Count
    Počet znaků v řetězci, která poskytuje počáteční hodnoty pro bity bitset.

  • _Zero
    Znak, který reprezentuje nulu.Výchozí hodnota je "0".

  • _One
    Znak, který reprezentuje některou.Výchozí je '1'.

Poznámky

Tři konstruktory lze vytvořit třídy obects bitset<N>:

  • První konstruktor žádné parametry, vytvoří objekt třídy bitset<N> a inicializuje všechny n bitů na výchozí hodnotu nula.

  • Druhý konstruktor sestaví objekt třídy bitset<N> a inicializuje bitů pomocí jednoho unsigned long long parametr.

  • Třetí konstruktor sestaví objekt třídy bitset<N>, inicializace n bitů hodnoty, které odpovídají znaků v řetězci znak c styl nuly a jedničky.Volání konstruktoru bez obsazení do řetězce typu řetězec:bitset<5> b5("01011");

Existují dvě šablony konstruktoru:

  • První šablona konstruktor sestaví objekt třídy bitset<N> a inicializuje bitů ze znaků v řetězci nuly a jedničky.Pokud jsou všechny znaky řetězce než 0 nebo 1, vyvolá konstruktor objekt třídy neplatný argument.Pokud zadané pozici (_Pos) přesahuje délku řetězce, a pak objekt třídy vyvolá konstruktor out_of_range.Konstruktor nastaví pouze tyto bity na pozici j v bitset, pro který znak v řetězci na pozici _Pos + j je 1.Ve výchozím nastavení _Pos je 0.

  • Druhý šablony konstruktoru je podobný první, ale zahrnuje další parametr (_Count), se používá k určení počtu bitů inicializovat.Má také dva volitelné parametry, _Zero a _One, které označují znaků, v _Str je vykládat rozumí 0 bit a 1 bit.

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 expresssion
   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;
}
  
  
  
  
  
  
  
  

Požadavky

Záhlaví: <bitset>

Obor názvů: std

Viz také

Referenční dokumentace

bitset Class