STL原始碼剖析(二)
阿新 • • 發佈:2018-12-12
opp(Object-Oriented Programming)vs GP(Generic Programming)
- OPP:企圖將datas和methods分開來
- GP:企圖將datas和methods分開來
- 分開的好處:
(1)containers和algorithms可各自閉門造車,其間以Iterator為媒介。
(2)algorithms通過iterators確定操作範圍,並通過iterator取用container元素。
- 容器自己沒有sort時才用全域性的sort,有sort時,用自己的。如list。
- 所有algoriothms,其內部最終涉及原屬本身的操作,無非就是比大小。
操作符過載(需要學習)
-
Class Templates類模板(需要學習)
-
Function Templates函式模板(需要學習)
-
Member Templates成員模板(目前不需要)
Specialization 特化
//泛化 template<class type> struct _type_traits{...} _type_traits<Foo> a; //特化1 template<> struct _type_traits<int> {...} _type_traits<int> b; //特化2 template<> struct _type_traits<double> {...} _type_traits<double> c;
partial specialization 偏特化
//泛化
template<class T,class Alloc=alloc> class vector {...}
//偏特化
template<class Alloc > class vector<bool,Alloc> {...}