// list結點基類
struct _list_node_base ;
// list結點
template
struct _list_node : public _list_node_base ;
// list迭代器,迭代器型別雙向迭代器
struct _list_iterator_base;
template
struct _list_iterator : public _list_iterator_base;
// _m_node指向list結點,每次迭代更新迭代器的_m_node的指向
_list_node_base* _m_node; // 指向list結點
// list記憶體分配
// 該類中儲存了記憶體分配器_node_allocator與所分配的_list_node結點記憶體的指標_m_node
template
class _list_alloc_base;
// _list_alloc_base的特化版本,不需儲存記憶體分配器(使用靜態的分配器),只有乙個資料成員_m_node
template
class _list_alloc_base<_tp, _allocator, true>;
// list基類
template
class _list_base
: public _list_alloc_base<_tp, _alloc,
_alloc_traits<_tp, _alloc>::_s_instanceless>
~_list_base()
void clear();
};// 清除量表中的所有元素
template
void _list_base<_tp,_alloc>::clear()
_m_node->_m_next = _m_node;
_m_node->_m_prev = _m_node;
}template
class
list : protected _list_base<_tp, _alloc>;
// list中_m_node是指向哨兵結點的
iterator begin()
// 注意swap只是交換哨兵結點的指標
void swap(list
<_tp, _alloc>& __x)
// 將[__first, __last)中的元素移到__position之前,splice的實現
void transfer(iterator __position, iterator __first, iterator __last)
}// 注意,unique是移除連續相等的元素
template
void
list
<_tp, _alloc>::unique()
}// merge兩個已排序的list,按公升序
template
void
list
<_tp, _alloc>::merge(list
<_tp, _alloc>& __x)
else
++__first1;
if (__first2 != __last2) transfer(__last1, __first2, __last2);
}// 沒弄懂reverse的實現
inline
void __list_base_reverse(_list_node_base* __p)
while (__tmp != __p);
}template
inline
void
list
<_tp, _alloc>::reverse()
// 歸併演算法實現list sort排序,保持相等元素的相對順序
template
void
list
<_tp, _alloc>::sort()
__carry.swap(__counter[__i]);
if (__i == __fill) ++__fill;
} for (int __i = 1; __i < __fill; ++__i)
__counter[__i].merge(__counter[__i-1]);
swap(__counter[__fill-1]);
}}
sgi stl
cppreference.com
STL 原始碼閱讀
1 這裡可以看出來,容器將迭代器作為類成員。vectora iteratorite a.begin 容器的成員函式可以返回迭代器,所以迭代器是容器的成員物件。2 個人理解,迭代器是對指標的封裝和提公升,盡可能遮蔽資料結構的底層細節,對外提供統一的操作介面,這些介面跟普通指標的功能類似,比如自增或自減...
STL原始碼閱讀 二
vector的記憶體分配基類 template class vector alloc base vector alloc base的偏特化版本,不需要儲存記憶體分配器 template class vector alloc base tp,allocator,true template struct...
STL原始碼閱讀 七
set使用紅黑樹實現,每個鍵值都不相同,且按序儲存。注意operator 即 rb tree的實現 先銷毀賦值號左邊的set,然後將右邊的set拷貝給左邊的set,而不是原值替換。set的所有函式都是用 rb tree的函式實現的,相當於 rb tree的乙個包裝類。multiset使用紅黑樹實現,...