GeekBand笔记-《STL与泛型编程 》 第三周

来源:互联网 发布:淘宝定价策略 编辑:程序博客网 时间:2024/05/29 10:33

变易算法

变易算法是指那些改变容器中对象的操作。具体包括:

copy template<class _InIt, class _OutIt> inline _OutIt copy(_InIt _First, _InIt _Last, _OutIt _Desc) // 将对象从 [_First, _Last) 拷贝至 [), 其中:_DestLast = _Dest+(_Last - _First).此算法是按照顺序拷贝的: _First, _First+1,..., _Last -1 // copy 可以实现将容器中的对象左移。swap template<class _Ty> inline void swap(_Ty& _Left, _Ty& _Right) // 交换对象 _Left 和 _Righttransform template<class _InIt, class _OutIt, class _Fn1> 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 - _First1replace template<class _FwdIt, class _Ty> inline void replace(_FwdIt _First, _FwdIt _Last, const _Ty& _Oldval, const _Ty& _Newval) // 对于区间 [_First, _Last)中每个迭代器it,如果满足 : *it == _Oldval, 则执行: *it = _Newvalfill template<class _FwdIt, class _Ty> inline void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) // 将 _Val赋值给 [_First, _Last) 中的每个元素generate template<class _FwdIt, class _Fn0> inline void generate(_FwdIt _First,_FwdIt _Last, _Fn0 _Func) // 将调用 _Func的结果赋值给 [_First, _Last)中每个元素remove template<class _FwdIt, class _Ty> inline _FwdIt remove(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) // 将[_First, _Last)中所有等于 _Val的元素全部删除。也就是说,remove返回一个迭代器_Last2(_Last2≤_Last),使得[_First,_Last2)中没有与_Val相等的元素。unique template<class _FwdIt> inline _FwdIt unique(_FwdIt _First, _FwdIt _Last) // 从[_First, _Last)去除重复的元素(只保留一份),返回一个新的迭代器 _Last2, 使得 [_First, _Last2)中没有重复出现的元素reserve template<class _BidIt> inline void reverse(_BidIt _First, _BidIt _Last) // 对于 0 ≤ it ≤ (_Last - _First) /2 中的每个元素,做如下交换: *(_First+it) <- -> *(_Last - (it + 1))rotatetemplate<class _FwdIt> inline_FwdIt rotate(_FwdIt _First, _FwdIt _Mid, _FwdIt _Last)// 交换区间 : [_First, _Mid) <- -> [_Mid, _Last)random_shuffletemplate<class _RanIt> inlinevoid random_shuffle(_RanIt _First, _RandIt _Last) // 采用rand// 对区间[_First, _Last)中的元素进行洗牌。如果令 N=_Last-_First, 那么该算法即从 N! 的可能排列中随机选择一种partitiontemplate<class _FwdIt, class _Pr> 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 算法。

相关算法接口如下:

stemplate inline
void sort(_RanIt _First, _RanIt _Last)
// 对[_First, _Last) 中的元素进行排序
// 对于被排序的元素,需要提供operator <
partial_sort

template inline
void partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last)
// 对 [_First, _Last) 中的部分元素进行排序,使得 [_First, _Mid) 中的元素是有序的, 而[_Mid, _Last) 中的元素则是未定义的
binary_search

template

accumulate template<class _InIt, class _Ty> inline _Ty accumulate(_InIt _First, _InIt _Last, _Ty _Val) // 对[_First, _Last) 中的每个元素进行累加,具体操作为:设result=_Val, 对于每个 it ∈[_First, _Last), result += *it // 也可以自定义result的处理函数,不仅仅是 +=。 template<class _InIt, class _Ty, class _Fn2> inline _Ty accumulate(_InIt _First, _InIt _Last, _Ty _Val, _Fn2 _Func) // 其中 result 的操作按照如下的操作进行。 result = _Func(*it, result)inner_product template<class _InIt1, class _InIt2, class _Ty> 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))partial_sum template<class _InIt, class _OutIt> inline _OutIt partial_sum(_InIt _First, _InIt _Last, _OutIt _Dest) //设 it=_First, *it = *_First, *(it+1)=*_First+*(_First+1), ..., 依此类推adjacent_difference template<class _InIt, class OutIt> 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_typeallocator::pointerallocator::const_pointerallocator::referenceallocator::const_referenceallocator::size_typeallocator::difference_typeallocator::rebind // 一个嵌套的 class template. class rebind<U> 拥有唯一成员 other,那么是一个typedef,代表 allocator<U>allocator::allocator() // default constructallocator::allocator(const allocator&) // copy constructortemplate<class U>allocator::allocator(const allocator<U>&) // 泛化的 copy constructorallocator::~allocator() // destructorpointer allocator::address(reference x)const // 返回某个const对象的地址。算式 a.address(x) 等同于 &x.const_pointer allocator::address(const_reference x)const // 返回某个const对象的地址pointer allocator::allocate(size_type n,const void* = 0) // 分配空间,足以存储n个T对象,第二个参数是个提示,实现上可能会利用它来增进区域性,或完全忽略之
void allocator::deallocate(pointer p,size_type n) // 释放先前分配的空间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()