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
bitset.htm
廣告