C++标准库(三)之STL算法

  • 算法头文件:
#include<algorithm>
#include<numeric>
  • 算法简介:STL算法采用覆盖模式而非安插模式,所以调用者必须保证有足够的操作空间。
  • 函数对象类型声明:
    • UnaryProc:Op(elem),无返回值
    • CompFunc:Op(elem1,elem2),返回True或者False
    • UnaryPredicate:Op(elem),返回True或者False
    • BinaryPredicate:Op(elem,value),返回True或者False

非更易型算法

  • 元素计数
UnaryProc for_each(InputIterator beg,InputIterator end,UnaryProc Op)   
difference_type count(InputIterator beg,InputIterator end,const T& val)    
difference_type count_if(InputIterator beg,InputIterator end,UnaryProc Op)    
  • 最小值与最大值
ForwardIterator min_element(ForwardIterator beg,ForwardIterator end)
ForwardIterator min_element(ForwardIterator beg,ForwardIterator end,CompFunc Op)
ForwardIterator max_element(ForwardIterator beg,ForwardIterator end)
ForwardIterator max_element(ForwardIterator beg,ForwardIterator end,CompFunc Op)
pair<ForwardIterator,ForwardIterator> minmax_element(ForwardIterator beg,ForwardIterator end)
pair<ForwardIteraror,ForwardIterator> minmax_element(ForwardIterator beg,ForwardIterator end,CompFunc Op)
  • 查找元素
//查找第一个匹配的元素
InputIterator find(InputIterator beg,InputIterator end,const T& value);
InputIterator find_if(InputIterator beg,InputIterator end,UnaryPredicate Op);
InputIterator find_if_not(InputIterator beg,InputIterator end,UnaryPredicate Op);

//查找前n个连续匹配的值
ForwardIterator search_n(ForwardIterator beg,ForwardIterator end,size count,const T& value);
ForwardIterator search_n(ForwardIterator beg,ForwardIterator end,size count,const T& value,BinaryPredicate Op);

//查找第一个子区间
ForwardIterator1 search(ForwardIterator1 beg,ForwardIterator1 end,ForwardIterator2 searchBeg,ForwardIterator2 searchEnd);
ForwardIterator1 search(ForwardIterator1 beg,ForwardIterator1 end,ForwardIterator2 searchBeg,ForwardIterator2 searchEnd,BinaryPredicate Op);

//查找最后一个子区间
ForwardIterator1 find_end(ForwardIterator1 beg,ForwardIterator1 end,ForwardIterator2 searchBeg,ForwardIterator2 searchEnd);
ForwardIterator1 find_end(ForwardIterator1 beg,ForwardIterator1 end,ForwardIterator2 searchBeg,ForwardIterator2 searchEnd,BinaryPredicate Op);

//查找某些元素第一次出现的地点
InputIterator find_first_of(InputIterator beg,InputIterator end,ForwardIterator searchBeg,ForwardIterator searchEnd);
InputIterator find_first_of(InputIterator beg,InputIterator end,ForwardIterator searchBeg,ForwardIterator searchEnd,BinaryPredicate Op);

//查找两个连续且相等的元素
ForwardIterator adjacent_find(ForwardIterator beg,ForwardIterator end);
ForwardIterator adjacent_find(ForwardIterator beg,ForwardIterator end,BinaryPredicate Op);
  • 区间的比较
//验证相等性
bool equal(InputIterator1 beg,InputIterator1 end,InputIterator2 cmpBeg);
bool equal(InputIterator1 beg,InputIterator1,end,InputIterator2 cmpBeg,BinaryPredicate op);

//测试不定序之相等性(数据存在的顺序无所谓)
bool is_permutation(ForwardIterator1 beg,ForwardIterator1 end,ForwardIterator2 beg);
bool id_permutation(ForwardIterator1 beg,ForwardIterator1 end,ForwardIterator2 beg,CompFunc op);

//查找第一次不同
pair<InputIterator1,InputIterator2> mismatch(InputIterator1 beg,InputIterator1,end,InputIterator2 cmpBeg);
pair<Inputiterator1,InputIterator2> mismatch(InputIterator1 beg,InputIterator1,end,Inputiterator2 cmpBeg,CompFunc op);

//检验是否排序
bool is_sorted(ForwardIterator beg,ForwardIterator end);
bool is_sorted(ForwardIterator beg,ForwardIterator end,BinaryPredicate op);
ForwardIterator is_sorted_until(ForwardIterator beg,ForwardIterator end);
ForwardIterator is_sorted_until(ForwardIterator beg,ForwardIterator end,BinaryPredicate op);

//检验是否形成Heap
bool is_heap(RandomAccessIterator beg,RandomAccessIterator end);
bool is_heap(RandomAccessIterator beg,RandomAccessIterator end);
RandomAccessIterator is_heap_until(RandomAccessIterator beg,RandomAccessIterator end);
RandomAccessIterator is_heap_until(RandomAccessIterator beg,RandomAccessIterator end,BinaryPredicate op);

//检验ALL,NONE,ANY
bool all_of(InputIterator beg,InputIterator end,UnaryPredicate op);
bool any_of(InputIterator beg,InputIterator end,UnaryPredicate op);
bool none_of(InputIterator beg,InputIterator end,unaryPredicate op);

更易型算法

//复制元素,返回目标区间第一个未被覆盖的元素
OutputIterator copy(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg);
OutputIterator copy_if(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg,UnaryPredicate op)
OutputIterator copy_n(InputIterator sourceBeg,Size num,OutputIterator destBeg);
BidirectionalIterator2 copy_backward(BidirectionalIterator1 sourceBeg,BidirectionalIterator1 sourceEnd,BidirectionalIterator2 destEnd);    //反向

//搬移元素
OutputIterator move(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg);
BidirectionalIterator2 move_backward(BidirectionalIterator1 sourceBeg,BidirectionIterator1 sourceEnd,BidircectionIterator2,destEnd);//反向

//单序列元素处理并写入目标区间
OutputIterator transform(InputIterator sourceBeg,InputIterator sourceEnd,outputIterator destBeg,UnaryFunc op);

//两序列元素结合并写入目标区间
OutputIterator transform(InputIterator1 sourceBeg,InputIterator1 sourceEnd,InputIterator2 sourceBeg,OutputIterator destBeg,BinaryFunc op);

//元素互换
ForwardIterator2 swap_ranges(ForwardIterator1 beg,ForwardIterator1 end,ForwardIterator2 beg2);

//元素赋值
void fill(ForwardIterator beg,ForwardIterator end,const T& elem);
void fill_n(Forwarditerator beg,Size num,const T& elem);

//替换元素
void replace(ForwardIterator beg,ForwardIterator end,const T& elem,const T& newValue);
void relplace_if(ForwardIterator beg,ForwardIterator end,UnaryPredicate op,const T& value);
OutputIterator replace_copy(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg,const T& oldValue,const T& newValue);
OutputIterator replace_copy_if(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg,UnaryPreicate op,const T& newValue);
  • 移除型算法
//移除序列内的某些元素
ForwardIterator remove(ForwardIterator beg,ForwardIterator end,const T& value);
ForwardIterator remove_if(ForwardIterator beg,ForwardIterator end,UnaryPredicate op);

//复制时一并移除某些元素
OutputIterator remove_copy(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg,const T& elem);
OutputIterator remove_copy_if(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg,UnaryPredicate op);

//移除连续重复元素
ForwardIterator unique(ForwardIterator beg,Forwarditerator end);
ForwardIterator unique(ForwardIterator beg,ForwardIterator end,BinaryPredicate op);

//复制过程中移除重复元素
OutputIterator unique_copy(InputIterator sourceBeg,InputIterator sourceBeg,OutputIterator destBeg);
OutputIterator unique_copy(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg,BinaryPredicate op);
  • 变序型算法
//反转元素次序
void reverse(BidirectionalIterator beg,BidiectionalIterator end);
void reverse_copy(BidirectionalIterator sourceBeg,BidirectionalIterator sourceEnd,OutputIterator destEnd);

//对容器内的元素重新洗牌
void random_shuffle(RandomAccessIterator beg,RandomIterator end);
  • 排序算法
//对所有元素排序
void sort(RandomAccessIterator beg,RandomAccessIterator end);
void sort(RandomAccessIterator beg,RandomAccessIterator end,BinaryPredicate op);
void stable_sort(RandomAccessIterator beg,RandomAccessIterator end,BinaryPredicate op);
void stable_sort(RandomAccessIterator beg,RandomAccessIterator end,BinaryPredicate op);

//局部排序(将beg到sortEnd的元素排序)
void partial_sort(RandomAccessIterator beg,RandomAccessIterator sortEnd,RandomAccessIterator end);
void partial_sort(RandomAccessIterator beg,RandomAccessIterator sortEnd,RandomSccessIterator end,BinaryPredicate op);

//Heap算法
void make_heap(RandomAccessIterator beg,RandomAccessIterator end);
void make_heap(RandomAccessIterator beg,RandomAccessIterator end,BinaryPredicate op);
void push_heap(RandomAccessIterator beg,RandomAccessIterator end);    //容器先push,heap再push_heap
void push_heap(RandomAccessIterator beg,RandomAccessIterator end,BinaryPredicate op);
void pop_heap(RandomAccessIterator beg,RandomAccessIterator end);    //heap再pop_heap,容器再pop
void pop_heap(RandomAccessIterator beg,RandomAccessIterator end,BinaryPredicate op);
  • 已排序区间算法
//查找元素
bool binary_search(ForwardIterator beg,ForwardIterator end,const T& value);
bool includes(InputIterator1 beg,InputIterator1 end,InputIterator2 searchBeg,InputIterator2 searchEnd);
bool lower_bound(ForwardIterator beg,ForwardIterator end,const T& value);    //返回第一个存在的位置
bool upper_bound(ForwardIterator beg,ForwardIterator end,const T& value);    //返回最后一个存在的位置
pair<ForwardIterator,ForwardIterator> equal_range(ForwardIterator beg,ForwardIterator end,const T& value);    //返回最后一个和第一个可能的位置
 
//合并元素
OutputIterator merge(InputIterator sourceBeg,InputIterator sourceEnd,InputIterator source2Beg,InputIterator source2End,OutputIterator destBeg);    //合并两个集合的元素,包括汇总,并集,交集的处理
OutputIterator set_union(InputIterator source1Beg,InputIterator source1End,InputIterator source2Beg,InputIterator source2End,OutputIterator destBeg);    //合并两个已排序集合的并集
OutputIterator set_intersection(InputIterator source1Beg,InputIterator source1End,InputIterator source2Beg,InputIterator source2End,OutputIterator destBeg);    //合并两个已排序元素的交集
OutputIterator set_difference(InputIterator source1Beg,InputIterator source1End,InputIterator source2Beg,InputIterator source2End,OutputIterator destBeg);    //合并两个已排序元素的差集
  • 数值算法
T accumulate(InputIterator beg,InputIterator end,T initValue);     //求和   
T inner_product(InputIterator1 beg,InputIterator1 end,InputIterator2 beg2,T initvalue);    //求积
OutputIterator partial_sum(InputIterator sourceBeg,InputIterator sourceEnd,OutputIterator destBeg);    //将相对数值转换为绝对数值

posted on 2018-06-16 19:05  ukernel  阅读(237)  评论(0编辑  收藏  举报

导航