Cpp-standard-library-cpp-bitset-operator-subscript-bool
提供:Dev Guides
C ++ビットセットライブラリ-operator []関数
説明
- C ++関数 *std
- bitset :: operator []* は、位置 `+ pos +`のビットの値を返します。
宣言
- 以下は、std
- bitsetヘッダーからのstd :: bitset :: operator []関数の宣言です。
C 98
bool operator[] (size_t pos) const;
パラメーター
*pos* -値がアクセスされるビットの位置。
戻り値
`+ pos +`からビット値を返します。
例外
`+ pos +`が無効な場合、このメソッドは未定義の動作を引き起こします。 それ以外の場合、例外が発生すると、すべてのオブジェクトは有効な状態のままになります。
例
- 次の例は、std
- bitset :: operator []関数の使用法を示しています。
#include <iostream>
#include <bitset>
using namespace std;
int main(void) {
bitset<4> b("1001");
for (int i = 0; i < 4; ++i)
cout << "In bitset b[" << i << "] = " << b[i] << endl;
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
In bitset b[0] = 1
In bitset b[1] = 0
In bitset b[2] = 0
In bitset b[3] = 1