STL原始碼分析 bitset

2022-07-12 06:00:13 字數 1710 閱讀 3557

bitset
bitset中stl中用於表示點陣圖的容器,它支援讀寫特定bit、從整數或字串生成bitset物件。bitset大小通過模板引數指定,一旦編譯器確定便無法變更,這一點與vector有差異。

bitset_base_bitset的派生類。_base_bitset中包含乙個長度_nw,型別unsigned long的陣列,其中_nw通過模板引數指定。在64位系統中,unsigned long長度為8 byte。

注意,這裡_nb不一定是8 * sizeof(unsigned)的整數倍,所以需要巨集__bitset_words對其進行向上取整操作。

templateclass bitset : private _base_bitset<__bitset_words(_nb)>

templatestruct _base_bitset

bitset(unsigned long __val) : _base_bitset<__bitset_words(_nb)>(__val)

_base_bitset(unsigned long __val)

void _m_do_sanitize()

template struct _sanitize

};__stl_template_null struct _sanitize<0>

};

explicit bitset(const basic_string& __s,

size_t __pos = 0,

size_t __n = basic_string::npos)

: _base()

const size_t __sub_offset = __bits_per_word - __offset;

for (size_t __n = _nw - 1; __n > __wshift; --__n)

_m_w[__n] = (_m_w[__n - __wshift] << __offset) |

(_m_w[__n - __wshift - 1] >> __sub_offset);

_m_w[__wshift] = _m_w[0] << __offset;

}

const size_t __sub_offset = __bits_per_word - __offset;

for (size_t __n = 0; __n < __limit; ++__n)

_m_w[__n] = (_m_w[__n + __wshift] >> __offset) |

(_m_w[__n + __wshift + 1] << __sub_offset);

_m_w[__limit] = _m_w[_nw-1] >> __offset;

留下問題給讀者思考:為何bitset中的陣列型別是unsigned long而非unsigned char?

推薦閱讀

STL原始碼分析set

include include using namespace std int main set iset ia,ia 5 cout size iset.size endl cout 3 count iset.count 3 endl iset.insert 3 cout size iset.siz...

STL原始碼分析 List

鍊錶是一種線性表,但不會按照線性的順序儲存。鍊錶每次插入和刪除乙個元素,只配置或者釋放乙個元素空間,對於任何位置的元素的插入或者刪除,list永遠是常量時間複雜度。template struct listnode 節點物件包含兩個節點物件指標,分別指向前乙個節點和後乙個節點,還有乙個節點物件存放的資...

STL原始碼分析 string

從定義可知,string其實是base string的特化類,string使用預設的記憶體分配器 stl default allocator chart template class alloc stl default allocator chart class basic string typed...