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的位置 j。默认情况下,_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 Class