GeekBand STLと汎型プログラミングThird Week
6809 ワード
GeekBand STLと汎型プログラミングThird Week
アルゴリズム
変換アルゴリズムとは、容器内のオブジェクトを変更する動作のことです.具体的には: copy swap transform replace fill ゲナート Remove unique レスリング rotate ランドドムshuffle partion 並べ替え
順序付けはアルゴリズムの中で最も基本的で重要なアルゴリズムである.C+++STLでは基本的なsortアルゴリズムが提供されている.
関連アルゴリズムインターフェースは以下の通りです. sort partial_ソト ビアーリsearch merge includes スタックベースのアルゴリズム 数値アルゴリズム
汎型数値アルゴリズムは、ヘッダファイルに含まれます. accumullate inner_product partial_sum adjacent_difference メモリ分配器
各STLの容器にはメモリ分配器があり、容器のメモリ割り当てと回収を管理する.C++のSTL容器はデフォルトではメモリ分配器がありますが、一般的には自分でallcatorを実現する必要はありません.デフォルトのメモリ分配器は十分に効率的で安定しています.
STL仕様により、以下はallocatorの必要なインターフェースとtypeタイプです.
allocator::value_タイプ
allocator:pointer
allocator:const_pointer
allocator:reference
allocator:const_reference
allocator::size_タイプ
allocator:difference_タイプ
allocator:rebind/一つの入れ子のクラスtemplate.class rebindは唯一のメンバーotherを持っています.それではtypedefです.allocatorを表します.
allocator::allocator()//default construct
allocator::allocator//copy constructor
templateallocator:allocator/一般化のcopy constructor
allocator:~allocator()/destructor
pointer allocator:address(reference x)const/は、あるconstオブジェクトのアドレスを返します.式a.address(x)は&xに等しいです.
constpointer allocator:address(constureference x)const/は、あるconstオブジェクトのアドレスに戻ります.
pointer allocator:allocate(sizautype、const void*=0)/分配空間は、n個のTオブジェクトを記憶するのに十分であり、第二のパラメータはヒントであり、それを利用して領域性を増進したり、完全に無視したりすることが可能である.
void allocator::deallocate(pointer p,sizautype)//前に割り当てられた空間を解放する
size_type allocator:max_size()const//リターンの割り当てに成功した最大値
void allocator:construct(pointer p,const T&x)//new(void*)p)T(x)に相当します.
void allocator::destroy(pointer p)//p->~T()に相当します.
アルゴリズム
変換アルゴリズムとは、容器内のオブジェクトを変更する動作のことです.具体的には:
template inline
_OutIt copy(_InIt _First, _InIt _Last, _OutIt _Desc)
// [_First, _Last) [), :_DestLast = _Dest+(_Last - _First). : _First, _First+1,..., _Last -1
// copy 。
template inline
void swap(_Ty& _Left, _Ty& _Right)
// _Left _Right
template inline
_OutIt transform(_InIt _First, _InIt _Last, _OutIt _Desc, _Fn1 _Func)
// [_First1,_Last) it, _Func(*it), _Dest , : *(_Desc+i) = _Func(*(_First+i)), 0 ≤ i < _Last1 - _First1
template inline
void replace(_FwdIt _First, _FwdIt _Last, const _Ty& _Oldval, const _Ty& _Newval)
// [_First, _Last) it, : *it == _Oldval, : *it = _Newval
template inline
void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
// _Val [_First, _Last)
template inline
void generate(_FwdIt _First,_FwdIt _Last, _Fn0 _Func)
// _Func [_First, _Last)
template inline
_FwdIt remove(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
// [_First, _Last) _Val 。 ,remove _Last2(_Last2≤_Last), [_First,_Last2) _Val 。
template inline
_FwdIt unique(_FwdIt _First, _FwdIt _Last)
// [_First, _Last) ( ), _Last2, [_First, _Last2)
template inline
void reverse(_BidIt _First, _BidIt _Last)
// 0 ≤ it ≤ (_Last - _First) /2 , : *(_First+it) *(_Last - (it + 1))
template inline
_FwdIt rotate(_FwdIt _First, _FwdIt _Mid, _FwdIt _Last)
// : [_First, _Mid) [_Mid, _Last)
template inline
void random_shuffle(_RanIt _First, _RandIt _Last) // rand
// [_First, _Last) 。 N=_Last-_First, N!
template inline
_FwdIt partition(_FwdIt _First, _FwdIt _Last, _Pr -Pred)
// _Pred , [_First,_Last) , [_First, _Mid) [_Mid, _Last), : it1 ∈ [_Mid, _Last), _Pred(*it2) == false
順序付けはアルゴリズムの中で最も基本的で重要なアルゴリズムである.C+++STLでは基本的なsortアルゴリズムが提供されている.
関連アルゴリズムインターフェースは以下の通りです.
template inline
void sort(_RanIt _First, _RanIt _Last)
// [_First, _Last)
// , operator <
template inline
void partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last)
// [_First, _Last) , [_First, _Mid) , [_Mid, _Last)
template inline
bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
// [_First, _Last) _Val
//
tempalte inline _OutIt merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Desc)
// [_First1, _Last1) [_First2, _Last2) _Dest
tempalte inline
bool includes(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2)
// [_First1, _Last1) [_First2, _Last2)
// [_First1, _Last1) [_First2, _Last2)
make_heap
push_heap
pop_heap
sort_heap
汎型数値アルゴリズムは、ヘッダファイルに含まれます.
template inline
_Ty accumulate(_InIt _First, _InIt _Last, _Ty _Val)
// [_First, _Last) , : result=_Val, it ∈[_First, _Last), result += *it
// result , +=。
template inline
_Ty accumulate(_InIt _First, _InIt _Last, _Ty _Val, _Fn2 _Func)
// result 。 result = _Func(*it, result)
template inline _Ty inner_product(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _Ty _Val)
// [_First1, _Last1) [_First2, _Last2) , result = _Val, it1 ∈[_First1, _Last1), result = result + (*it1) ✖️ *(_First2 + (it1 - _First1))
template inline _OutIt
partial_sum(_InIt _First, _InIt _Last, _OutIt _Dest)
// it=_First, *it = *_First, *(it+1)=*_First+*(_First+1), ...,
template inline
_OutIt adjacent_difference(_InIt _First, _InIt _Last, _OutIt _Dest)
// *result=*_First, it ∈[_First +1, _Last), *it *(it-1) *(result+(it-_First))
各STLの容器にはメモリ分配器があり、容器のメモリ割り当てと回収を管理する.C++のSTL容器はデフォルトではメモリ分配器がありますが、一般的には自分でallcatorを実現する必要はありません.デフォルトのメモリ分配器は十分に効率的で安定しています.
STL仕様により、以下はallocatorの必要なインターフェースとtypeタイプです.
allocator::value_タイプ
allocator:pointer
allocator:const_pointer
allocator:reference
allocator:const_reference
allocator::size_タイプ
allocator:difference_タイプ
allocator:rebind/一つの入れ子のクラスtemplate.class rebindは唯一のメンバーotherを持っています.それではtypedefです.allocatorを表します.
allocator::allocator()//default construct
allocator::allocator//copy constructor
templateallocator:allocator/一般化のcopy constructor
allocator:~allocator()/destructor
pointer allocator:address(reference x)const/は、あるconstオブジェクトのアドレスを返します.式a.address(x)は&xに等しいです.
constpointer allocator:address(constureference x)const/は、あるconstオブジェクトのアドレスに戻ります.
pointer allocator:allocate(sizautype、const void*=0)/分配空間は、n個のTオブジェクトを記憶するのに十分であり、第二のパラメータはヒントであり、それを利用して領域性を増進したり、完全に無視したりすることが可能である.
void allocator::deallocate(pointer p,sizautype)//前に割り当てられた空間を解放する
size_type allocator:max_size()const//リターンの割り当てに成功した最大値
void allocator:construct(pointer p,const T&x)//new(void*)p)T(x)に相当します.
void allocator::destroy(pointer p)//p->~T()に相当します.