オブジェクトを構築し、クラスの 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 のビットを初期化するには、基数 2 の表示に使用される符号なし整数。_Str
bitset のビット値の初期化に使われる The string ゼロと物。_CStr
bitset のビット値を初期化するために使用するゼロと物における. の式の文字列。_Pos
左から右へ見なされ、ゼロで始まる文字列の文字の位置 bitset の最初のビットを初期化するために使用します。_Count
bitset ビットに初期値を指定するために使用する文字列の文字数。_Zero
ゼロを表すために使用される文字。既定値は 「0 " です。_One
1 を表すために使用される文字。既定値は 「1 " です。
解説
3 人のコンストラクターがクラス **bitset<N>**の obects の構築に使用できる:
一つ目のコンストラクターは、パラメーターを受け取らないし、bitset<N>、クラスのオブジェクトを構築し、およびの既定値にすべての N 個のビットを初期化します。
2 つ目のコンストラクターは、クラス bitset<N> オブジェクトを構築し、unsigned long long の単一のパラメーターを使用してビットを初期化します。
3 つ目のコンストラクターは、クラス **bitset<N>**の N 個のビットを初期化する、および物の C. の式の文字列に設定される文字に対応する値にオブジェクトを構築します。文字列型に文字列をキャスト コンストラクターを呼び出します: bitset<5> b5("01011");
提供される 2 種類のコンストラクターのテンプレートもあります:
一つ目のコンストラクターは bitset<N> テンプレート クラスのオブジェクトを構築し、ゼロの一つの文字列で指定した文字のビットを初期化します。文字列のいずれかの文字が 0 または 1 以外の場合、コンストラクターは、クラス 無効な引数のオブジェクトをスローします。指定された位置が文字列の長さを超えて_Pos () の場合、コンストラクターは、クラス out_of_rangeのオブジェクトをスローします。コンストラクターは位置の文字列の文字が _Pos + j 1. である bitset の位置で の それらのビットが設定されます。既定では、_Pos は 0 です。
2 番目のコンストラクターのテンプレートは、1 番目のに似ていますが、ビット数を指定する初期化するために使用される追加のパラメーター (_Count) が含まれます。また、0 ビットと 1 個意味するには、_Str のどの文字が解釈されるかを示す 2 _Zero、省略可能なパラメーターと _Oneそれぞれがあります。
使用例
// 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