課上版本GNU2.91
OOP面向?qū)ο缶幊膛cGP泛型編程
?????? OOP:數(shù)據(jù)和操作放在一起????
?????? GP:將數(shù)據(jù)和操作分開來
如果容器自帶sort則用容器自帶的
算法涉及元素本身的操作就是比大小
相當于是重新定義了一個操作將之替換為=原來默認的<
技術(shù)基礎:操作符重載and模板(泛化 全特化 偏特化)
? ? 不可重載的操作符
_list_iterater
類模板(定義類)
函數(shù)模板(定義函數(shù))
注:template<typename T>和template<class T>功能相同
成員模板
特化specialization
template<class type>//泛化
struct __type_traits{}
template<> struct ?__type_traits<int>{}//特化
partial Specialization
? ? 個數(shù)上的偏————有兩個模板參數(shù) 綁定其中一個
范圍上的偏————
分配器allocators(不建議使用)
? ? operator new() 和 malloc()
VC6所附的標準庫署尤,其allocator實現(xiàn)如下()
int*p = allocator<int>().allocate(512,(int*)0);//分配
allocator<int>.deallocate(p,512);//還
BC5對allocator的使用(容器使用分配器)
? ? 所有容器第二個模板參數(shù)都是class Allocator = allocator<T>
BC5所附的標準庫求厕,其allocator實現(xiàn)如下
注:用法和vc一樣 ?但vc沒有默認值----BC---int*p = allocator().allocate(512);//分配
G2.9所附的標準庫佑附,其allocator實現(xiàn)如下
注:若果區(qū)塊大 則開銷所占比例小,而現(xiàn)實中通常區(qū)塊小
G4.9所附的標準庫峦朗,其allocator實現(xiàn)如下
operate new 調(diào)用 malloc
operate delete 調(diào)用free
4.9中的__pool_alloc就是2.9中的alloc
容器 -- 結(jié)構(gòu)與分類
g2.9幾乎沒有繼承
注:slist名為forward_list,hash_set名為unordered_set
容器 list(2.9--4 vs 4.9--8)
所有的容器除了vector之外和array之外都必須是class
list的iterator設計
所有的容器都要有typedef 必須是iterator
? ? type++的設計 :1.記錄原址self tmp = *this; (不會喚起operator*,其實是喚起copy ctor用以創(chuàng)建tmp并以*this 為初值 不會喚起ope 因為*this已被解釋為ctor的參數(shù)) 2.進行操作 ++*this; 3.返回原址return tmp;
注:c++不允許后++兩次 故itrator前++寫成self& operator++() {node = (link_type)...... ? (前++沒有&)
Iterator設計原則和 iterator traits的作用和設計
iterator traits——用于萃取iterator特性,那算法如何獲知迭代器的屬性呢?這一任務就是traits完成的肤视。在STL實現(xiàn)中,traits編程技術(shù)得到大量的運用涉枫,它利用了“內(nèi)嵌類型”的編程技巧與C++的template參數(shù)推導功能邢滑,彌補了C++類型識別方面的不足。通過traits拜银,算法可以原汁原味的將迭代器的屬性萃取出來殊鞭,幫助算法正確高效的運行。
Iterator需要遵循的原則
Iterator需要回答Algorithms的五種問題
? ? ? ? 1.iterator_category ? ?2.difference_type ? ?3.value_type ? ?4.reference ? ?5.pointer(4 5沒有用過)
1.iterator_traits——用以分離class iterators 和non-iterator