标准非STL容器 : bitset
什么是“標(biāo)準(zhǔn)非STL容器”?標(biāo)準(zhǔn)非STL容器是指“可以認(rèn)為它們是容器,但是他們并不滿(mǎn)足STL容器的所有要求”。前文提到的容器適配器stack、queue及priority_queue都是標(biāo)準(zhǔn)非STL容器的一部分。此外,valarray也是標(biāo)準(zhǔn)非STL容器。
bitset:一種高效位集合操作容器。
2. API
bitset提供的api:
(constructor)?? ?Construct bitset (public member function)
operator[]?? ?Access bit (public member function)
set?? ?Set bits (public member function)
reset?? ?Reset bits (public member function )
flip?? ?Flip bits (public member function)
to_ulong?? ?Convert to unsigned long integer (public member function)
to_string?? ?Convert to string (public member function)
count?? ?Count bits set (public member function)
size?? ?Return size (public member function)
test?? ?Return bit value (public member function )
any?? ?Test if any bit is set (public member function)
none?? ?Test if no bit is set (public member function)
3. 源碼剖析
SGI bitset部分實(shí)現(xiàn)源碼
節(jié)選上述代碼,可以得到:
1. bitset繼承_Base_bitset,具體操作封裝在_Base_bitset中
2. bitset 的size作為模板參數(shù)(非類(lèi)型模板參數(shù)的一個(gè)要求是,編譯器能在編譯期就能把參數(shù)確定下來(lái)),因此,bitset大小在編譯期固定,不支持插入和刪除元素
3. 各種位操作,性能高
4._Base_bitset使unsigned long作為底層存儲(chǔ),不支持指針、引用、迭代器
5. 使用?_WordT _M_w[_Nw];分配內(nèi)存,因此在棧中定義bitset需要注意大小(和STL標(biāo)準(zhǔn)容器堆內(nèi)存分配區(qū)別開(kāi))。
???? eg,下面的代碼將棧溢出(測(cè)試機(jī)器棧內(nèi)存10M)? [cpp]?view plaincopy
[cpp]?view plaincopy
bitset高效,但是size必須在編譯器確定,不支持插入和刪除。因此,一個(gè)可能的替代品是vector<bool>和deque<bool>
兩者的區(qū)別:
vector<bool>不是一個(gè)STL容器,并且不容納bool(like bitse底層t機(jī)制)
deque<bool>是一個(gè)STL容器,它保存真正的bool值
分別運(yùn)行
[cpp]?view plaincopy
[cpp]?view plaincopy
使用deque<bool>正確,而是用vector<bool>會(huì)報(bào)錯(cuò):“cannot convert `std::_Bit_reference*' to `bool*' in initialization“
但是,deque簡(jiǎn)直是在踐踏內(nèi)存。
使用deque<bool>
[cpp]?view plaincopy
? PID USER????? PR? NI? VIRT? RES? SHR S %CPU %MEM??? TIME+? COMMAND????????????????????????????????????????????????????????????? ?
23612 work????? 25?? 0?9990m?9.8g? 720 S? 0.0 65.0?? 0:39.35 test
使用vector<bool>
[cpp]?view plaincopy
? PID USER????? PR? NI? VIRT? RES? SHR S %CPU %MEM??? TIME+? COMMAND????????????????????????????????????????????????????????????? ?
23909 work????? 25?? 0?1198m?1.2g? 716 S? 0.0? 7.8?? 0:01.31 test
使用bitset
[cpp]?view plaincopy
? PID USER????? PR? NI? VIRT? RES? SHR S %CPU %MEM??? TIME+? COMMAND????????????????????????????????????????????????????????????? ?
24439 work????? 25?? 0?1198m?1.2g? 712 S 30.7? 7.8?? 0:00.92 test??
10億個(gè)bool,vector<bool>和bitset使用內(nèi)存1198M,deque<bool>則是9990M
5. 總結(jié)
在需要對(duì)位集合進(jìn)行操作的時(shí)候,如何操作集合大小比較固定,優(yōu)先選擇高效的bitset;
如果需要?jiǎng)討B(tài)增刪元素,或者編譯期間無(wú)法確定集合大小,則可以考慮vector<bool>,deque<bool>內(nèi)存開(kāi)銷(xiāo)太大,基本上不考慮。
參考:
http://www.sgi.com/tech/stl/download.html
http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/bitset/
擴(kuò)展閱讀:
Vector specialization:?vector<bool>
The vector class template has a special template specialization for the?bool?type.This specialization is provided to optimize for space allocation: In this template specialization, each element occupies only one bit (which is eight times less than the smallest type in C++:char).
The references to elements of a?bool?vector returned by the?vector?members are not references tobool?objects, but a special member type which is a reference to a single bit, defined inside thevector<bool>?class specialization as:
[cpp]?view plaincopy
轉(zhuǎn)載于:https://www.cnblogs.com/freeopen/p/5483005.html
總結(jié)
以上是生活随笔為你收集整理的标准非STL容器 : bitset的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何获取真实的执行计划
- 下一篇: Memcached缓存实例