天天看點

C++ STL bitset 用法

C++的 bitset 在 bitset 頭檔案中,它是一種類似數組的結構,它的每一個元素隻能是0或1,每個元素僅用1bit空間。

下面是具體用法

構造函數

bitset常用構造函數有四種,如下

bitset<4> bitset1;  //無參構造,長度為4,預設每一位為0

    bitset<8> bitset2(12);  //長度為8,二進制儲存,前面用0補充

    string s = "100101";
    bitset<10> bitset3(s);  //長度為10,前面用0補充
    
    char s2[] = "10101";
    bitset<13> bitset4(s2);  //長度為13,前面用0補充

    cout << bitset1 << endl;  //0000
    cout << bitset2 << endl;  //00001100
    cout << bitset3 << endl;  //0000100101
    cout << bitset4 << endl;  //0000000010101
           

注意:

用字元串構造時,字元串隻能包含 '0' 或 '1' ,否則會抛出異常。

構造時,需在<>中表明bitset 的大小(即size)。

在進行有參構造時,若參數的二進制表示比bitset的size小,則在前面用0補充(如上面的栗子);若比bitsize大,參數為整數時取後面部分,參數為字元串時取前面部分(如下面栗子):

bitset<2> bitset1(12);  //12的二進制為1100(長度為4),但bitset1的size=2,隻取後面部分,即00

    string s = "100101";  
    bitset<4> bitset2(s);  //s的size=6,而bitset的size=4,隻取前面部分,即1001

    char s2[] = "11101";
    bitset<4> bitset3(s2);  //與bitset2同理,隻取前面部分,即1110

    cout << bitset1 << endl;  //00
    cout << bitset2 << endl;  //1001
    cout << bitset3 << endl;  //1110
           

可用的操作符

bitset對于二進制有位操作符,具體如下

bitset<4> foo (string("1001"));
    bitset<4> bar (string("0011"));

    cout << (foo^=bar) << endl;       // 1010 (foo對bar按位異或後指派給foo)
    cout << (foo&=bar) << endl;       // 0010 (按位與後指派給foo)
    cout << (foo|=bar) << endl;       // 0011 (按位或後指派給foo)

    cout << (foo<<=2) << endl;        // 1100 (左移2位,低位補0,有自身指派)
    cout << (foo>>=1) << endl;        // 0110 (右移1位,高位補0,有自身指派)

    cout << (~bar) << endl;           // 1100 (按位取反)
    cout << (bar<<1) << endl;         // 0110 (左移,不指派)
    cout << (bar>>1) << endl;         // 0001 (右移,不指派)

    cout << (foo==bar) << endl;       // false (0110==0011為false)
    cout << (foo!=bar) << endl;       // true  (0110!=0011為true)

    cout << (foo&bar) << endl;        // 0010 (按位與,不指派)
    cout << (foo|bar) << endl;        // 0111 (按位或,不指派)
    cout << (foo^bar) << endl;        // 0101 (按位異或,不指派)
           

此外,可以通過 [ ] 通路元素(類似數組),注意最低位下标為0.

可用函數

bitset還支援一些有意思的函數,比如:

bitset<8> foo ("10011011");

    cout << foo.count() << endl;  //5  (count函數用來求bitset中1的位數,foo中共有5個1
    cout << foo.size() << endl;   //8  (size函數用來求bitset的大小,一共有8位

    cout << foo.test(0) << endl;  //true  (test函數用來查下标處的元素是0還是1,并傳回false或true,此處foo[0]為1,傳回true
    cout << foo.test(2) << endl;  //false  (同理,foo[2]為0,傳回false

    cout << foo.any() << endl;  //true  (any函數檢查bitset中是否有1
    cout << foo.none() << endl;  //false  (none函數檢查bitset中是否沒有1
    cout << foo.all() << endl;  //false  (all函數檢查bitset中是全部為1
           

補充說明一下:test函數會對下标越界作出檢查,而通過 [ ] 通路元素卻不會經過下标檢查,是以,在兩種方式通用的情況下,選擇test函數更安全一些

另外,含有一些函數:

bitset<8> foo ("10011011");

    cout << foo.flip(2) << endl;  //10011111  (flip函數傳參數時,用于将參數位取反,本行代碼将foo下标2處"反轉",即0變1,1變0
    cout << foo.flip() << endl;   //01100000  (flip函數不指定參數時,将bitset每一位全部取反

    cout << foo.set() << endl;    //11111111  (set函數不指定參數時,将bitset的每一位全部置為1
    cout << foo.set(3,0) << endl;  //11110111  (set函數指定兩位參數時,将第一參數位的元素置為第二參數的值,本行對foo的操作相當于foo[3]=0
    cout << foo.set(3) << endl;    //11111111  (set函數隻有一個參數時,将參數下标處置為1

    cout << foo.reset(4) << endl;  //11101111  (reset函數傳一個參數時将參數下标處置為0
    cout << foo.reset() << endl;   //00000000  (reset函數不傳參數時将bitset的每一位全部置為0
           

同樣,它們也都會檢查下标是否越界,如果越界就會抛出異常

最後,還有一些類型轉換的函數,如下:

bitset<8> foo ("10011011");

    string s = foo.to_string();  //将bitset轉換成string類型
    unsigned long a = foo.to_ulong();  //将bitset轉換成unsigned long類型
    unsigned long long b = foo.to_ullong();  //将bitset轉換成unsigned long long類型

    cout << s << endl;  //10011011
    cout << a << endl;  //155
    cout << b << endl;  //155
           

原部落格:https://www.cnblogs.com/magisk/p/8809922.html