STL常见算法

这么长时间过去了,一点点的总算将STL中常见的算法看完了,是时候总结一下了。

一、for_each,允许对区间内的元素进行修改

二、元素计数

  1. difference_type count (InputIterator beg, InputIterator end, const T& value);
  2. difference_type count_if (InputIterator beg, InputIterator end, UnaryPredicate op)

三 、查找

  1. InputIterator find (InputIterator beg, InputIterator end, const T& value);
  2. InputIterator find_if (InputIterator beg, InputIterator end, UnaryPredicate op);
  3. InputIterator find_if_not (InputIterator beg, InputIterator end, UnaryPredicate op)
  4. ForwardIterator1 search (ForwardIterator1 beg, ForwardIterator1 end,ForwardIterator2 searchBeg, ForwardIterator2 searchEnd);//查找beg-end区间内与第二个区间完全一致的子区间,返回查找到子区间的第一个元素的位置。
  5. ForwardIterator1 search (ForwardIterator1 beg, ForwardIterator1 end,ForwardIterator2 searchBeg, ForwardIterator2 searchEnd,BinaryPredicate op);//查找beg-end区间内与第二个区间完全一致的子区间,返回查找到子区间的第一个元素的位置。
  6. InputIterator find_first_of (InputIterator beg, InputIterator end,ForwardIterator searchBeg, ForwardIterator searchEnd);
  7. InputIterator find_first_of (InputIterator beg, InputIterator end,ForwardIterator searchBeg, ForwardIterator searchEnd,BinaryPredicate op);//查找某些元素第一次出现的地点
  8. ForwardIterator adjacent_find (ForwardIterator beg, ForwardIterator end);
  9. ForwardIterator adjacent_find (ForwardIterator beg, ForwardIterator end,BinaryPredicate op);//查找两个连续且相等的元素。

四、非更易型算法

     1. bool equal (InputIterator1 beg, InputIterator1 end,InputIterator2 cmpBeg);

     2. bool equal (InputIterator1 beg, InputIterator1 end,InputIterator2 cmpBeg,BinaryPredicate op);//校验两个区间是否相等

     3. bool is_permutation (ForwardIterator1 beg1, ForwardIterator1 end1,ForwardIterator2 beg2);

     4. bool is_permutation (ForwardIterator1 beg1, ForwardIterator1 end1,ForwardIterator2 beg2,CompFunc op);//检测两个区间,在不考虑数序的的情况下的相等性。

     5. pair<InputIterator1,InputIterator2>mismatch (InputIterator1 beg, InputIterator1 end,InputIterator2 cmpBeg);

     6. pair<InputIterator1,InputIterator2>mismatch (InputIterator1 beg, InputIterator1 end,InputIterator2 cmpBeg,BinaryPredicate op);//返回两个区间内第一个不同的元素组成的pair。如果都相同则返回两个区间内最后一个元素组成的pair。

     7. bool lexicographical_compare (InputIterator1 beg1, InputIterator1 end1,InputIterator2 beg2, InputIterator2 end2);

     8. bool lexicographical_compare (InputIterator1 beg1, InputIterator1 end1,InputIterator2 beg2,InputIterator2 end2,CompFunc op);//以字典顺序,比较两个区间,第一个用“<”比较,第二种使用op比较。

    9. bool is_sorted(ForwardIterator beg, ForwardIterator end);

    10.bool is_sorted (ForwardIterator beg, ForwardIterator end, BinaryPredicate op);//检测一个区间是否是有序的,如果区间内只有一个元素或者没有元素都返回真。

    11. ForwardIterator is_sorted_until (ForwardIterator beg, ForwardIterator end);

    12. ForwardIterator is_sorted_until (ForwardIterator beg, ForwardIterator end, BinaryPredicate op);//如果区间是无序的则返回第一个破坏顺序位置,否则返回end。

    13. bool is_partitioned (InputIterator beg, InputIterator end, UnaryPredicate op);//返回一个区间是否被切割,切割的意思就是所有使op返回true的元素都在返回false的元素之前。

    14. ForwardIterator partition_point (ForwardIterator beg, ForwardIterator end, BinaryPredicate op);//返回切割后的第一个元素的位置。

    15. bool all_of (InputIterator beg, InputIterator end, UnaryPredicate op);

    16. bool any_of (InputIterator beg, InputIterator end, UnaryPredicate op);

    17. 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  transform (InputIterator1 source1Beg, InputIterator1 source1End,OutputIterator destBeg,BinaryFunc op);

OutputIterator transform (InputIterator1 source1Beg, InputIterator1 source1End,InputIterator2 source2Beg,OutputIterator destBeg,BinaryFunc op);

ForwardIterator2 swap_ranges (ForwardIterator1 beg1, ForwardIterator1 end1,ForwardIterator2 beg2);

void fill (ForwardIterator beg, ForwardIterator end,const T& newValue)

void fill_n (OutputIterator beg, Size num,const T& newValue)

void generate (ForwardIterator beg, ForwardIterator end,Func op)

void generate_n (OutputIterator beg, Size num,Func op);

void iota (ForwardIterator beg, ForwardIterator end,T startValue);//卧槽 这个傻逼函数我觉的有必要记下

int main()
{
    array<int,10> coll;
    iota (coll.begin(), coll.end(),42); 
    PRINT_ELEMENTS(coll,"coll: ");
}
//The program has the following output:
//coll: 42 43 44 45 46 47 48 49 50 51

void replace (ForwardIterator beg, ForwardIterator end,const T& oldValue, const T& newValue)

void replace_if (ForwardIterator beg, ForwardIterator end,UnaryPredicate op, const T& newValue)

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,UnaryPredicate 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& value)

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)

 

void reverse (BidirectionalIterator beg, BidirectionalIterator end)

OutputIterator reverse_copy (BidirectionalIterator sourceBeg, BidirectionalIterator sourceEnd,OutputIterator destBeg)

ForwardIterator rotate (ForwardIterator beg, ForwardIterator newBeg, ForwardIterator end)

OutputIterator rotate_copy (ForwardIterator sourceBeg, ForwardIterator newBeg,ForwardIterator sourceEnd,OutputIterator destBeg)//旋转

bool next_permutation (BidirectionalIterator beg, BidirectionalIterator end)

bool next_permutation (BidirectionalIterator beg, BidirectionalIterator end,BinaryPredicate op)

bool prev_permutation (BidirectionalIterator beg, BidirectionalIterator end)

bool prev_permutation (BidirectionalIterator beg, BidirectionalIterator end,BinaryPredicate op)

这个有点难理解,首先需要先明白什么是字典序,比如说我们有1,2,3三个数,按字典序排序,序列就是123,132,213,231,312,321,所以当使用next_permutation操作132时,得到的将是213,如果是prev_permutation操作312得到的将是231。可以使用该函数排序,但是效率真的差到没朋友,所以非常不建议使用该方法排序。

 

//重排序

void shuffle (RandomAccessIterator beg, RandomAccessIterator end,UniformRandomNumberGenerator&& eng)

void random_shuffle (RandomAccessIterator beg, RandomAccessIterator end)

void random_shuffle (RandomAccessIterator beg, RandomAccessIterator end,RandomFunc&& op)

//stable 的意思是稳定的,partition划分。将使OP返回true的元素移到区间的前部

ForwardIterator partition (ForwardIterator beg, ForwardIterator end,UnaryPredicate op)

BidirectionalIterator stable_partition (BidirectionalIterator beg, BidirectionalIterator end,UnaryPredicate op)

pair<OutputIterator1,OutputIterator2> partition_copy (InputIterator sourceBeg, InputIterator sourceEnd,OutputIterator1 destTrueBeg, OutputIterator2 destFalseBeg,UnaryPredicate op)

//不能对list和forward_list使用,因为它们两个没有提供随机访问迭代器

//复杂度 nlog(n)

 

void sort (RandomAccessIterator beg, RandomAccessIterator end)//排序

void sort (RandomAccessIterator beg, RandomAccessIterator end, BinaryPredicate op)

 

//如果内存充足,复杂度也是nlog(n) 否则是nlog(n) * log(n)

void stable_sort (RandomAccessIterator beg, RandomAccessIterator end)

void stable_sort (RandomAccessIterator beg, RandomAccessIterator end,BinaryPredicate op)

 

//局部排序复杂度在线性与nlog(n)之间

void partial_sort (RandomAccessIterator beg, RandomAccessIterator sortEnd,RandomAccessIterator end)

void partial_sort (RandomAccessIterator beg, RandomAccessIterator sortEnd,RandomAccessIterator end, BinaryPredicate op)

RandomAccessIterator partial_sort_copy (InputIterator sourceBeg, InputIterator sourceEnd,RandomAccessIterator destBeg, RandomAccessIterator destEnd)

RandomAccessIterator partial_sort_copy (InputIterator sourceBeg, InputIterator sourceEnd,RandomAccessIterator destBeg, RandomAccessIterator destEnd,BinaryPredicate op)

 

//只保证nth前面的比nth小,后面的比nth大,不保证相对顺序,但是在有些编译器中结果也是全排列的,但是千万不要依赖该特性,如果真的要全排序的话请使用sort函数,或者partial_sort

void nth_element (RandomAccessIterator beg, RandomAccessIterator nth,RandomAccessIterator end)

void nth_element (RandomAccessIterator beg, RandomAccessIterator nth,RandomAccessIterator end, BinaryPredicate op)

 

堆算法:

bool is_heap (RandomAccessIterator beg, RandomAccessIterator end);

RandomAccessIterator is_heap_until (RandomAccessIterator beg, RandomAccessIterator end);//返回第一个使区间不再是heap的元素的位置。

void make_heap (RandomAccessIterator beg, RandomAccessIterator end)

void make_heap (RandomAccessIterator beg, RandomAccessIterator end,BinaryPredicate op)

//该算法是将区间内的最后一个元素添加到heap中,所以调用中必须确保beg~end-1已经是一个heap

void push_heap (RandomAccessIterator beg, RandomAccessIterator end)

void push_heap (RandomAccessIterator beg, RandomAccessIterator end,BinaryPredicate op)

//pop出堆中第一个元素(最大的),将最大的元素移动到最后一个位置,并且将剩下的元素组织成一个新的heap,同样调用该函数之前必须确保beg-end区间就是一个heap

void pop_heap (RandomAccessIterator beg, RandomAccessIterator end)

void pop_heap (RandomAccessIterator beg, RandomAccessIterator end,BinaryPredicate op)

//对heap进行排序,从此之后beg-end区间将不再是一个heap

void sort_heap (RandomAccessIterator beg, RandomAccessIterator end)

void sort_heap (RandomAccessIterator beg, RandomAccessIterator end,BinaryPredicate op)

 

//处理已经排序的算法

//二分查找法,如果是随机访问迭代器则是对数复杂度,否则是线性

bool binary_search (ForwardIterator beg, ForwardIterator end, const T& value)

bool binary_search (ForwardIterator beg, ForwardIterator end, const T& value,BinaryPredicate op)//op为排序规则

 

//判断两个已排序区间2是不是区间1的子集

bool includes (InputIterator1 beg, InputIterator1 end,InputIterator2 searchBeg, InputIterator2 searchEnd)

bool includes (InputIterator1 beg, InputIterator1 end,InputIterator2 searchBeg, InputIterator2 searchEnd,BinaryPredicate op)

 

//小于等于value的第一个元素

ForwardIterator lower_bound (ForwardIterator beg, ForwardIterator end, const T& value)

ForwardIterator lower_bound (ForwardIterator beg, ForwardIterator end, const T& value,BinaryPredicate op)

//大于value的第一个元素

ForwardIterator upper_bound (ForwardIterator beg, ForwardIterator end, const T& value)

ForwardIterator upper_bound (ForwardIterator beg, ForwardIterator end, const T& value,BinaryPredicate op)

 

//查找等于value的区间,如果是随机访问迭代器是对数复杂度,否则是线性复杂度

pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator beg, ForwardIterator end, const T& value)

pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator beg, ForwardIterator end, const T& value,BinaryPredicate op)

//合并两个已排序区间

OutputIterator merge (InputIterator source1Beg, InputIterator source1End,

InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg)

OutputIterator merge (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg, BinaryPredicate op)

//合并两个已排序区间,如果一个元素在两个区间内同时出现,则在结果中只会有一份,如果两个源区间中的元素本身就有多个,则目标区间中该元素的个数与两个区间中个数多个保持一致。

OutputIterator set_union (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg)

OutputIterator set_union (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg, BinaryPredicate op)

 

//求两个已排序区间的交集,如果两个区间内同一个元素出现多次,则在目标区间中则也会出现该元素多次

OutputIterator set_intersection (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg)

OutputIterator set_intersection (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg, BinaryPredicate op)

//求两个区间中元素的差集,如果一个元素在两个区间中都出现,且出现次数不同,则结果中该元素的个数等于第一个区间中的个数减去第二个区间中的个数,如果第二个区间中出现的次数更多,则结果中将不会出现该元素

OutputIterator set_difference (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg)

OutputIterator set_difference (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg, BinaryPredicate op)

 

//求在两个区间中只在第一个区间但不在第二个区间或者只在第二个区间不在第一个区间中的元素,如果一个元素在两个区间中出现多次,目标区间中该元素的个数等于,该元素在两个区间中多的减去少的个数

OutputIterator set_symmetric_difference (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg)

OutputIterator set_symmetric_difference (InputIterator source1Beg, InputIterator source1End,InputIterator source2Beg, InputIterator source2End,OutputIterator destBeg, BinaryPredicate op)

 

//在某一个位置合并两个区间

void inplace_merge (BidirectionalIterator beg1, BidirectionalIterator end1beg2,BidirectionalIterator end2)

void inplace_merge (BidirectionalIterator beg1, BidirectionalIterator end1beg2,BidirectionalIterator end2, BinaryPredicate op)

例:

vector<int>v4(20);
iota(v4.begin(), v4.begin() + 10, 0);
iota(v4.begin() + 10,v4.end(), 0);
inplace_merge(v4.begin(), v4.begin() + 10, v4.end());
copy(v4.begin(), v4.end(), ostream_iterator<int>(cout, " "));
//结果:0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

//求和,在initValue的基础上累加

T accumulate (InputIterator beg, InputIterator end,T initValue)

T accumulate (InputIterator beg, InputIterator end,T initValue, BinaryFunc op)

 

//两个区间内各个元素相乘并累加到initValue中

T inner_product (InputIterator1 beg1, InputIterator1 end1,InputIterator2 beg2, T initValue)

T inner_product (InputIterator1 beg1, InputIterator1 end1,InputIterator2 beg2, T initValue,BinaryFunc op1, BinaryFunc op2)

//相对值转换为绝对值

OutputIterator partial_sum (InputIterator sourceBeg, InputIterator sourceEnd,OutputIterator destBeg)

OutputIterator partial_sum (InputIterator sourceBeg, InputIterator sourceEnd,OutputIterator destBeg, BinaryFunc op)

• Thus, for the values

a1 a2 a3 ...

they compute either

a1, a1 + a2, a1 + a2 + a3, ...

or

a1, a1 op a2, a1 op a2 op a3, ...

 

//绝对值转换为相对值

OutputIterator adjacent_difference (InputIterator sourceBeg, InputIterator sourceEnd,OutputIterator destBeg)

OutputIterator adjacent_difference (InputIterator sourceBeg, InputIterator sourceEnd,OutputIterator destBeg, BinaryFunc op)

• Thus, for the values

a1 a2 a3 a4 ...

they compute and write either the values

a1, a2 - a1, a3 - a2, a4 - a3, ...

or the values

a1, a2 op a1, a3 op a2, a4 op a3, ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值