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

本文探讨了C++ STL中的变易算法,重点介绍了排序(sort)和部分排序(partial_sort)的功能及使用,同时提到了二分搜索(binary_search)的相关接口,这些算法在处理容器中的元素时起到关键作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

变易算法

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

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 和 _Right
transform

 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 - _First1
replace

 template<class _FwdIt, class _Ty> inline
 void replace(_FwdIt _First, _FwdIt _Last, const _Ty& _Oldval, const _Ty& _Newval)
 // 对于区间 [_First, _Last)中每个迭代器it,如果满足 : *it == _Oldval, 则执行: *it = _Newval
fill

 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))
rotate

template<class _FwdIt> inline
_FwdIt rotate(_FwdIt _First, _FwdIt _Mid, _FwdIt _Last)
// 交换区间 : [_First, _Mid) <- -> [_Mid, _Last)
random_shuffle

template<class _RanIt> inline
void random_shuffle(_RanIt _First, _RandIt _Last) // 采用rand
// 对区间[_First, _Last)中的元素进行洗牌。如果令 N=_Last-_First, 那么该算法即从 N! 的可能排列中随机选择一种
partition

template<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_type

allocator::pointer

allocator::const_pointer

allocator::reference

allocator::const_reference

allocator::size_type

allocator::difference_type

allocator::rebind // 一个嵌套的 class template. class rebind<U> 拥有唯一成员 other,那么是一个typedef,代表 allocator<U>

allocator::allocator() // default construct

allocator::allocator(const allocator&) // copy constructor

template<class U>allocator::allocator(const allocator<U>&) // 泛化的 copy constructor

allocator::~allocator() // destructor

pointer 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值