共用方式為


bitset::bitset

建構物件類別 bitset<N> 並初始化位元為零個,或對某些指定值,則會從字串中的字元取得的值。

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’)
);

參數

  • _Val
    基底兩個表示用來初始化在建構的 bitset 位元的不帶正負號的整數。

  • _Str
    用於的零和一的字串初始化 bitset 位元值。

  • _CStr
    用於的零和一個 C. 字串初始化 bitset 位元值。

  • _Pos
    在字串,由左至右計數和開始以零起始的字元位置,用來初始化在 bitset 的第一個位元組。

  • _Count
    字元數是用來為 bitset 的位元提供初始值的字串。

  • _Zero
    用來表示零字元。 預設值為「0 "。

  • _One
    用來表示後面的字元。 預設值為「1 "。

備註

三個建構函式可以用來建構 obects 類別: bitset<N>

  • 第一個建構函式不接受參數,建構物件類別 bitset<N> 並初始化所有 N 位元為預設值零。

  • 使用單一 unsigned long long 參數,第二個建構函式所建構物件類別 bitset<N> 並初始化位元。

  • 第三個建構函式所建構物件類別 bitset<N>,初始化 N 位元對應於在零和一的 C++. 式提供字串的字元值。 您呼叫建構函式,而轉換為字串的資料型別: bitset<5> b5("01011");

也有提供的兩個建構函式樣板:

  • 第一個建構函式樣板建構物件類別 bitset<N> 並從檔案連接在零和一個字串的字元的位元。 除了 0 或 1 以外,如果字串的任何字元為,建構函式會擲回類別 無效的引數物件。 如果指定的位置 (_Pos) 是字串的長度,則建構函式會擲回類別 out_of_range物件。 建構函式將那些位元在字串中的字元位置 _Pos + j 為 1. 的 bitset 位置 。 根據預設, _Pos 為 0。

  • 第二個建構函式範本類似於第一個,不過,包括用來指定位元組數目初始化的額外參數 (_Count)。 它分別也有兩個選擇性參數、 _Zero 和 _One,表示 _Str 所表示提示字元會解譯 0 位元和 1 位元處理。

範例

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

需求

標題: <bitset>

命名空間: std

請參閱

參考

bitset 類別