原创 STL之仿函數,配接器實現

#ifndef __FUNCTIONAL_H #define __FUNCTIONAL_H template<class Arg,class Result> struct unary_function { typedef Arg arg

原创 STL之numeric實現

#ifndef __NUMERIC_H #define __NUMERIC_H #include"iterator_traits.h" template<class InputIterator,class T> T accumulate(

原创 STL之hashtable實現

#ifndef __HASH_FUN_H #define __HASH_FUN_H template<class Key> struct __hash{}; inline size_t __stl_hash_string(const

原创 單件模式

有一些對象我們只需要一個,比如線程池、緩存、對話框等等,如果製造出多個實例,就會導致許多問題產生。 單件模式利用一個靜態變量來記錄Singleton類的唯一實例;把構造器聲明爲私有的,只有自Singleton類內部纔可以調用構造器;用ge

原创 策略模式

針對接口編程,而不是針對實現編程。找出應用中可能需要變化的地方,把它們獨立出來,不要和那些需要變化的代碼混在一起(把會變化的部分取出並“封裝”起來,好讓其它部分不受影響)。 在下面的實例中,將fly和quack方法用獨立的接口實現,比如F

原创 STL之map實現

#ifndef __MAP_H #define __MAP_H #include"rbt.h" #include"pair.h" #include<functional> template<class Key,class T,class

原创 STL之智能指針

#ifndef __MEMORY_H #define __MEMORY_H #include"Allocator.h" #include"construct.h" #include"uninitialized.h" template<c

原创 STL之迭代器實現

#ifndef __ITERATOR_H #define __ITERATOR_H #include"iterator_traits.h" #include<iostream> template<class Container> clas

原创 STL之heap實現

#ifndef __HEAP_H #define _HEAP_H #include"iterator_traits.h" template<class RandomAccessIterator> inline void mypush_he

原创 觀察者模式

觀察者模式定義了對象之間的一對多依賴,這樣一來,當一個對象改變狀態時,它的所有依賴者都會收到通知並自動更新。觀察者依賴於此主題,只要主題狀態一有變化,觀察者就會被通知,根據通知的風格,觀察者可能因此新值而更新。關於觀察者的一切,主題只知道

原创 工廠方法模式

工廠方法模式定義了一個創建對象的接口,但由子類決定要實例化的類是哪一個,工廠方法讓類把實例化推遲到子類。 #ifndef __FACTORY_H #define __FACTORY_H #include<iostream> #inclu

原创 適配器模式

適配器模式將一個類接口,轉換成客戶期望的另一個接口,讓原本接口不兼容的類可以合作無間。 #ifndef __ADAPTER_H #define __ADAPTER_H #include<iostream> using namespace

原创 裝飾者模式

利用繼承設計子類行爲,是在編譯時靜態決定的,而且所有的子類都會繼承到相同的行爲,而如果能利用組合的做法擴展對象的行爲,就可以在運行時動態地進行擴展。通過動態地組合對象,可以寫新的代碼添加新功能,而無須修改現有的代碼,這樣可以使得引入bug

原创 STL之set實現

#ifndef __SET_H #define __SET_H #include"rbt.h" #include<functional> template<class Key,class Compare=less<Key>,class

原创 STL之hashset實現

#ifndef __HASHSET_H #define __HASHSET_H #include"hash_fun.h" #include"hashtable.h" #include<functional> template<class