C++容器 bitset
生活随笔
收集整理的這篇文章主要介紹了
C++容器 bitset
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C++語言的一個類庫,用來方便地管理一系列的bit位而不用程序員自己來寫代碼。
要使用bitset 類我們必須包含相關(guān)的頭文件????#include <bitset>
bitset除了可以訪問指定下標(biāo)的bit位以外,還可以把它們作為一個整數(shù)來進(jìn)行某些統(tǒng)計。 可以如下聲明一個該類型變量: bitset<N>varm (M) 其中varm為變量名。 N表示該類型在內(nèi)存中占的位數(shù),是二進(jìn)制。 M表示變量varm的初始值。
#include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);string bitval("01011");bitset<5> mybits(bitval);cout<<boolalpha;for(size_t i=0;i<mybits.size();++i)cout<<mybits.test(i)<<endl;//返回指定位的狀態(tài)return 0; }
我們就可以做到這一點.例如: bitvec.reset();// 把所有的位設(shè)置為0
#include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);bitset<4> mybits(string("1011"));cout<<mybits.reset(2)<<endl;//1001cout<<mybits.reset()<<endl;//0000return 0; }
11.operatorss()
比較和賦值運算
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<4>first(string("1001"));bitset<4>second(string("0011"));cout<<(first^=second)<<endl;//1010cout<<(first&=second)<<endl;//0010cout<<(first|=second)<<endl;//0011cout<<endl;cout<<(first<<=2)<<endl;//1100cout<<(first>>=1)<<endl;//0110cout<<endl;cout<<(~second)<<endl;//1100cout<<(second<<1)<<endl;//0110cout<<(second>>1)<<endl;//0001cout<<endl;cout<<(first==second)<<endl;//falsecout<<(first!=second)<<endl;//truecout<<endl;cout<<(first&second)<<endl;//0010cout<<(first|second)<<endl;//0111cout<<(first^second)<<endl;//0101cout<<endl;return 0;
}
bitset除了可以訪問指定下標(biāo)的bit位以外,還可以把它們作為一個整數(shù)來進(jìn)行某些統(tǒng)計。 可以如下聲明一個該類型變量: bitset<N>varm (M) 其中varm為變量名。 N表示該類型在內(nèi)存中占的位數(shù),是二進(jìn)制。 M表示變量varm的初始值。
相關(guān)函數(shù):
1.any():
為了測試bitset 對象是否含有被設(shè)置為1的位,我們可以使用any()操作 當(dāng)bitset對象的一位或多個位被設(shè)置為1 時any()返回true 例如,對于bitvec ,如下測試 bool is_set = bitvec.any(); 它的結(jié)果當(dāng)然是false。 #include<iostream> #include<bitset> using namespace std;int main() {ios::sync_with_stdio(0);bitset<32>mybits;cin>>mybits;if(mybits.any())//任何一位被設(shè)置就返回truecout<<"mybits has "<<(int)mybits.count()<<"bits set\n";elsecout<<"No bit set\n";return 0; }2.none():
相反,如果bitset 對象的所有位都被設(shè)置為0 ,則none()操作返回true 例如,對于bitvec 測試 bool is_not_set = bitvec.none(); 結(jié)果為true #include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);bitset<32>mybits;cin>>mybits;if(mybits.none())//沒有任何一位被設(shè)置就返回truecout<<"No bit set\n";elsecout<<"mybits has "<<(int)mybits.count()<<"bits set\n";return 0; }3.count():
count()操作返回被設(shè)置為1的位的個數(shù). int bits_set = bitvec.count(); #include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);bitset<8> myset(string("10110011"));cout<<int(myset.count())<<" ones\n";cout<<int(myset.size()-myset.count())<<" zeros\n";return 0; }4.set():
我們可以用set()操作或者下標(biāo)操作符來設(shè)置某個單獨的位 例如,下面的for循環(huán)把下標(biāo)為偶數(shù)的設(shè)置為1. for ( int index = 0; index < 32; ++ index ) if ( index % 2 == 0 ) bitvec[ index ] = 1; #include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);bitset<4> mybits;cout<<mybits.set()<<endl;//1111cout<<mybits.set(2,0)<<endl;//1011cout<<mybits.set(2)<<endl;//1111return 0; }5.test():
測試某個單獨的位的狀態(tài)#include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);string bitval("01011");bitset<5> mybits(bitval);cout<<boolalpha;for(size_t i=0;i<mybits.size();++i)cout<<mybits.test(i)<<endl;//返回指定位的狀態(tài)return 0; }
6.reset():
要將某個單獨的位設(shè)置為0 ,我們可以用reset()或下標(biāo)操作符 下列兩個操作都將bitvec的第一位設(shè)為0. // 兩者等價都把第一位設(shè)置為0 bitvec.reset( 0 ); bitvec[ 0 ] = 0; 我們也可以用set()和reset()操作將整個bitset 對象的所有位設(shè)為1 或0 ,只要調(diào)用相應(yīng)的操作而不必傳遞位置參數(shù),我們就可以做到這一點.例如: bitvec.reset();// 把所有的位設(shè)置為0
#include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);bitset<4> mybits(string("1011"));cout<<mybits.reset(2)<<endl;//1001cout<<mybits.reset()<<endl;//0000return 0; }
7.flip():
flip()操作翻轉(zhuǎn)整個bitset 對象或一個獨立的位 bitvec.flip( 0 ); // 翻轉(zhuǎn)第一位 bitvec[0].flip(); // 也是翻轉(zhuǎn)第一位 bitvec.flip(); // 翻轉(zhuǎn)所有的位的值8.to_ulong();
返回bitset的整數(shù)表示
#include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);//string bitval("01011");string bitval="01011";bitset<5> mybits(bitval);cout<<mybits.to_ulong()<<endl;//返回bitset的整數(shù)表示return 0; }9.to_string():
返回bitset的字符串表示
#include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);bitset<5> mybits;//0000mybits.set();//1111string s=mybits.to_string();cout<<s<<endl;//返回bitset的字符串表示return 0; }10.operator[]()
返回第x位的引用
#include<iostream> #include<bitset> #include<cstdio> using namespace std;int main() {ios::sync_with_stdio(0);bitset<5> mybits;//0000mybits[1]=1;mybits[2]=mybits[1];cout<<mybits<<endl;return 0; }11.operatorss()
比較和賦值運算
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<4>first(string("1001"));bitset<4>second(string("0011"));cout<<(first^=second)<<endl;//1010cout<<(first&=second)<<endl;//0010cout<<(first|=second)<<endl;//0011cout<<endl;cout<<(first<<=2)<<endl;//1100cout<<(first>>=1)<<endl;//0110cout<<endl;cout<<(~second)<<endl;//1100cout<<(second<<1)<<endl;//0110cout<<(second>>1)<<endl;//0001cout<<endl;cout<<(first==second)<<endl;//falsecout<<(first!=second)<<endl;//truecout<<endl;cout<<(first&second)<<endl;//0010cout<<(first|second)<<endl;//0111cout<<(first^second)<<endl;//0101cout<<endl;return 0;
}
總結(jié)
以上是生活随笔為你收集整理的C++容器 bitset的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 6186 CS Course
- 下一篇: 51NOD 1138 连续整数的和