【C++】bitset 二進位制位的有序集
阿新 • • 發佈:2018-11-04
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<4> foo ("1011");
cout << foo[0] << endl; //1
cout << foo[1] << endl; //1
cout << foo[2] << endl; //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