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
    用于的零和一个用 . 式字符串初始化 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 j 的位置。 默认情况下,_Pos 为 0。

  • 第二个构造函数模板类似于第一个,即,但包括指定的位数初始化的其他参数 (_Count)。 它分别还有两个可选参数、_Zero 和 _One中,_Str 指示在哪些字符被解释表示 0 bit 一个用于 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 类