STL 常用算法

现代C++程序设计比较推崇使用模板STL。STL是C++中一个非常重要的改革。在C++03之后,STL表现非常不错,STL中提供了一些常见的算法。这些算法可以直接使用,而不需要自己再去写,效率。

简单总结一下STL一些比较好的算法。

一. 非变易算法

1. for_each:遍历容器元素进行操作

该函数的源代码:

template<class InputIterator, class Function>

Function for_each(InputIterator first, InputIterator last, Function f)

{

for ( ; first!=last; ++first ) f(*first);

return f;

}

其中f可以是普通的函数,也可以是函数对象。

 

2. find:查找容器的元素

函数源代码:

template<class InputIterator, class T>

InputIterator find ( InputIterator first, InputIterator last, const T& value )

{

for ( ;first!=last; first++) if ( *first==value ) break;

return first;

}

 

3. find_if: 条件查找容器的元素

函数源代码:

template<class InputIterator, class Predicate>

InputIterator find_if ( InputIterator first, InputIterator last,Predicate pred )

{

for ( ; first!=last ; first++ ) if ( pred(*first) ) break;

return first;

}

pred是一个函数对象。

 

4. adjacent_find:邻近查找容器元素

函数声明:

template <class ForwardIterator>

ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last );

 

template <class ForwardIterator, class BinaryPredicate>

ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last, BinaryPredicate pred );

函数代码:

template <class ForwardIterator>

ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last )

{

if (first != last)

{

ForwardIterator next=first; ++next;

while (next != last) {

if (*first == *next) // or: if (pred(*first,*next)), for the pred version

return first;

else { ++first; ++next; }

}

}

return last;

}

 

5. find_first_of范围查找容器元素

查找首次出现的元素

函数声明:

template <class ForwardIterator1, class ForwardIterator2>

ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2 );

 

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>

ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2,

BinaryPredicate pred );

 

函数源代码:

template<class ForwardIterator1, class ForwardIterator2>

ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2)

{

for ( ; first1 != last1; ++first1 )

for (ForwardIterator2 it=first2; it!=last2; ++it)

if (*it==*first1) // or: if (comp(*it,*first)) for the pred version

return first1;

return last1;

}

6. count:统计等于某值的容器元素的个数

函数源代码:

template <class InputIterator, class T>

ptrdiff_t count ( InputIterator first, InputIterator last, const T& value )

{

ptrdiff_t ret=0;

while (first != last) if (*first++ == value) ++ret;

return ret;

}

7. count_if:条件统计容器元素个数

函数源代码:

template <class InputIterator, class Predicate>

ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred )

{

ptrdiff_t ret=0;

while (first != last) if (pred(*first++)) ++ret;

return ret;

}

8. mismatch:元素不匹配查找

比较两个序列,找出首个不匹配的元素的位置。通过配对对象pair返回这两个元素的迭代器,指示不匹配元素的位置。

函数声明:

template <class InputIterator1, class InputIterator2>

pair<InputIterator1, InputIterator2>

mismatch (InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2 );

 

template <class InputIterator1, class InputIterator2, class BinaryPredicate>

pair<InputIterator1, InputIterator2>

mismatch (InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, BinaryPredicate pred );

函数源代码:

template <class InputIterator1, class InputIterator2>

pair<InputIterator1, InputIterator2>

mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )

{

while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for the pred version

{ ++first1; ++first2; }

return make_pair(first1,first2);

}

9. equal元素相等判断

类似mismatch,equal也是逐一比较两个序列的元素是否相等,只是equal函数返回true/false,不返回迭代器值。

函数声明:

template <class InputIterator1, class InputIterator2>

bool equal ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2 );

 

template <class InputIterator1, class InputIterator2, class BinaryPredicate>

bool equal ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, BinaryPredicate pred );

函数源代码:

template <class InputIterator1, class InputIterator2>

bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )

{

while ( first1!=last1 )

{

if (!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for pred version

return false;

++first1; ++first2;

}

return true;

}

10. search:子序列搜索

在一个序列中搜索与另一个序列匹配的子序列。返回子序列的首个元素的迭代器值,返回last1表示没有匹配的子序列。

函数声明:

template <class ForwardIterator1, class ForwardIterator2>

ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2 );

 

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>

ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2.

BinaryPredicate pred );

函数源代码:

template<class ForwardIterator1, class ForwardIterator2>

ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2)

{

if (first2==last2) return first1; // specified in C++11

 

while (first1!=last1)

{

ForwardIterator1 it1 = first1;

ForwardIterator2 it2 = first2;

while (*it1==*it2) { // or: while (pred(*it1,*it2)) for the pred version

++it1; ++it2;

if (it2==last2) return first1;

if (it1==last1) return last1;

}

++first1;

}

return last1;

}

11. search_n: 重复元素子序列搜索

搜索序列中是否有一系列元素值均为某个给定值的子序列。返回子序列的首个元素的迭代器,返回last表示没有重复的子序列。

函数声明:

template <class ForwardIterator, class Size, class T>

ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,

Size count, const T& value );

 

template <class ForwardIterator, class Size, class T, class BinaryPredicate>

ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,

Size count, const T& value, BinaryPredicate pred );

函数源代码:

template<class ForwardIterator, class Size, class T>

ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,

Size count, const T& value )

{

ForwardIterator it, limit;

Size i;

 

limit=first; advance(limit,distance(first,last)-count);

 

while (first!=limit)

{

it = first; i=0;

while (*it==value) // or: while (pred(*it,value)) for the pred version

{ ++it; if (++i==count) return first; }

++first;

}

return last;

}

 

12. find_end:最后一个子序列搜索

在一个序列中搜索出最后一个与另一个序列匹配的子序列。返回值为首元素的迭代器或返回last1,表示没有子序列匹配。

函数声明:

template <class ForwardIterator1, class ForwardIterator2>

ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2 );

 

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>

ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2,

BinaryPredicate pred );

函数源代码:

template<class ForwardIterator1, class ForwardIterator2>

ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2)

{

if (first2==last2) return last1; // specified in C++11

 

ForwardIterator1 ret = last1;

 

while (first1!=last1)

{

ForwardIterator1 it1 = first1;

ForwardIterator2 it2 = first2;

while (*it1==*it2) { // or: while (pred(*it1,*it2)) for the pred version

++it1; ++it2;

if (it2==last2) { ret=first1; break; }

if (it1==last1) return ret;

}

++first1;

}

return ret;

}

 

二、变易算法

1. copy元素复制

用于容器间元素的匹配。

函数源代码:

template<class InputIterator, class OutputIterator>

OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )

{

while (first!=last) *result++ = *first++;

return result;

}

2. copy_backward反向复制

函数源代码:

template<class BidirectionalIterator1, class BidirectionalIterator2>

BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,

BidirectionalIterator1 last,

BidirectionalIterator2 result )

{

while (last!=first) *(--result) = *(--last);

return result;

}

3. swap元素交换

这个操作非常的常见。

函数源代码:

template <class T> void swap ( T& a, T& b )

{

T c(a); a=b; b=c;

}

4. iter_swap迭代器交换

这是swap算法的迭代器形式。

函数源代码:

template <class ForwardIterator1, class ForwardIterator2>

void iter_swap ( ForwardIterator1 a, ForwardIterator2 b )

{

swap (*a, *b);

}

5. swap_ranges区间元素交换

用于两个迭代器区间元素的交换。

函数源代码:

template<class ForwardIterator1, class ForwardIterator2>

ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2 )

{

while (first1!=last1) swap(*first1++, *first2++);

return first2;

}

6. transform元素变换

用于实现容器元素的变换操作。

函数声明:

template < class InputIterator, class OutputIterator, class UnaryOperator >

OutputIterator transform ( InputIterator first1, InputIterator last1,

OutputIterator result, UnaryOperator op );

 

template < class InputIterator1, class InputIterator2,

class OutputIterator, class BinaryOperator >

OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, OutputIterator result,

BinaryOperator binary_op );

函数源代码:

template < class InputIterator, class OutputIterator, class UnaryOperator >

OutputIterator transform ( InputIterator first1, InputIterator last1,

OutputIterator result, UnaryOperator op )

{

while (first1 != last1)

*result++ = op(*first1++); // or: *result++=binary_op(*first1++,*first2++);

return result;

}

 

7. replace替换

将区间上的指定元素替换为新值。

函数源代码:

template < class ForwardIterator, class T >

void replace ( ForwardIterator first, ForwardIterator last,

const T& old_value, const T& new_value )

{

for (; first != last; ++first)

if (*first == old_value) *first=new_value;

}

8. replace_if条件替换

函数源代码:

template < class ForwardIterator, class Predicate, class T >

void replace_if ( ForwardIterator first, ForwardIterator last,

Predicate pred, const T& new_value )

{

for (; first != last; ++first)

if (pred(*first)) *first=new_value;

}

9. replace_copy替换和复制

先进行元素替换,再将元素复制到新容器。

函数源代码:

template < class InputIterator, class OutputIterator, class T >

OutputIterator replace_copy ( InputIterator first, InputIterator last,

OutputIterator result, const T& old_value, const T& new_value )

{

for (; first != last; ++first, ++result)

*result = (*first==old_value)? new_value: *first;

return result;

}

10. replace_copy_if条件替换和复制

函数源代码:

template < class InputIterator, class OutputIterator, class Predicate, class T >

OutputIterator replace_copy_if ( InputIterator first, InputIterator last,

OutputIterator result, Predicate pred,

const T& new_value )

{

for (; first != last; ++first, ++result)

*result = (pred(*first))? new_value: *first;

return result;

}

11. fill填充

将同一个值填充到容器的一个或多个元素处。

函数源代码:

template < class ForwardIterator, class T >

void fill ( ForwardIterator first, ForwardIterator last, const T& value )

{

while (first != last) *first++ = value;

}

12. fill_n n次填充

函数源代码:

template < class OutputIterator, class Size, class T >

void fill_n ( OutputIterator first, Size n, const T& value )

{

for (; n>0; --n) *first++ = value;

}

13. generate随机生成元素

为容器生成新元素。

函数源代码:

template <class ForwardIterator, class Generator>

void generate ( ForwardIterator first, ForwardIterator last, Generator gen )

{

while (first != last) *first++ = gen();

}

这儿的gen是一个函数对象,可以根据自己的需要进行设计。可以利用rand函数来产生随机数。为容器添加随机数。

14. generate_n随机生成n个元素

函数源代码:

template <class OutputIterator, class Size, class Generator>

void generate_n ( OutputIterator first, Size n, Generator gen )

{

for (; n>0; --n) *first++ = gen();

}

15. remove移除

将容器中等于某个给定值的元素全部移除掉。

函数源代码:

template < class ForwardIterator, class T >

ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value )

{

ForwardIterator result = first;

for ( ; first != last; ++first)

if (!(*first == value)) *result++ = *first;

return result;

}

16. remove_if条件移除

函数源代码:

template < class ForwardIterator, class Predicate >

ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,

Predicate pred )

{

ForwardIterator result = first;

for ( ; first != last; ++first)

if (!pred(*first)) *result++ = *first;

return result;

}

17. remove_copy移除复制

实质上是一个条件复制,将原容器中不等于某个给定值的元素复制到新容器。

函数源代码:

template <class InputIterator, class OutputIterator, class T>

OutputIterator remove_copy ( InputIterator first, InputIterator last,

OutputIterator result, const T& value )

{

for ( ; first != last; ++first)

if (!(*first == value)) *result++ = *first;

return result;

}

18. remove_copy_if条件移除复制

函数源代码:

template <class InputIterator, class OutputIterator, class Predicate>

OutputIterator remove_copy_if ( InputIterator first, InputIterator last,

OutputIterator result, Predicate pred )

{

for ( ; first != last; ++first)

if (!pred(*first)) *result++ = *first;

return result;

}

19. unique剔除连续重复元素

函数原型:

template <class ForwardIterator>

ForwardIterator unique ( ForwardIterator first, ForwardIterator last );

 

template <class ForwardIterator, class BinaryPredicate>

ForwardIterator unique ( ForwardIterator first, ForwardIterator last,

BinaryPredicate pred );

函数源代码:

template <class ForwardIterator>

ForwardIterator unique ( ForwardIterator first, ForwardIterator last )

{

ForwardIterator result=first;

while (++first != last)

{

if (!(*result == *first)) // or: if (!pred(*result,*first)) for the pred version

*(++result)=*first;

}

return ++result;

}

20. unique_copy不连续重复元素复制

函数原型:

template <class InputIterator, class OutputIterator>

OutputIterator unique_copy ( InputIterator first, InputIterator last,

OutputIterator result );

 

template <class InputIterator, class OutputIterator, class BinaryPredicate>

OutputIterator unique_copy ( InputIterator first, InputIterator last,

OutputIterator result, BinaryPredicate pred );

函数源代码:

template <class InputIterator, class OutputIterator>

OutputIterator unique_copy ( InputIterator first, InputIterator last,

OutputIterator result )

{

typename std::iterator_traits<InputIterator>::value_type value = *first;

*result=*first;

while (++first != last)

{

if (!(value == *first)) // or: if (!pred(value,*first)) for the pred version

*(++result) = value = *first;

}

return ++result;

}

 

21. reverse元素反向

函数源代码:

template <class BidirectionalIterator>

void reverse ( BidirectionalIterator first, BidirectionalIterator last)

{

while ((first!=last)&&(first!=--last))

swap (*first++,*last);

}

22. reverse_copy反向复制

函数源代码:

template <class BidirectionalIterator, class OutputIterator>

OutputIterator reverse_copy ( BidirectionalIterator first,

BidirectionalIterator last, OutputIterator result )

{

while (first!=last) *result++ = *--last;

return result;

}

23. rotate旋转

函数源代码:

template <class ForwardIterator>

void rotate ( ForwardIterator first, ForwardIterator middle,

ForwardIterator last )

{

ForwardIterator next = middle;

while (first!=next)

{

swap (*first++,*next++);

if (next==last) next=middle;

else if (first == middle) middle=next;

}

}

 

24. rotate_copy旋转复制

通过复制的方法实现旋转,比rotate算法更加简单和高效,但需要占用更多的内存空间。

函数代码:

template <class ForwardIterator, class OutputIterator>

OutputIterator rotate_copy ( ForwardIterator first, ForwardIterator middle,

ForwardIterator last, OutputIterator result )

{

result=copy (middle,last,result);

return copy (first,middle,result);

}

25. random_shuffle随机抖动

对容器内的元素进行随机排列

函数原型:

function template

random_shuffle

<algorithm>

 

template <class RandomAccessIterator>

void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last );

 

template <class RandomAccessIterator, class RandomNumberGenerator>

void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,

RandomNumberGenerator& rand );

函数源代码:

template <class RandomAccessIterator, class RandomNumberGenerator>

void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,

RandomNumberGenerator& rand )

{

iterator_traits<RandomAccessIterator>::difference_type i, n;

n = (last-first);

for (i=n-1; i>0; --i) swap (first[i],first[rand(i+1)]);

}

 

26. random_sample随机采样

对区间元素进行随机采样。这个函数不在STL中,是SGI STL中添加的。

函数源代码:

template <class InputIter, class RandomAccessIter,

class RandomNumberGenerator, class Distance>

RandomAccessIter random_sample(InputIter first, InputIter last,

RandomAccessIter out, RandomNumberGenerator& rand, const Distance n){

Distance m=0;

Distance t=n;

for(;first!=last&&m<n;++m,++first)

out[m]=*first;

while(first!=last){

++t;

Distance M=rand(t);

if(M<n)

out[M]=*first;

++first;

}

return out+m;

}

 

template <class InputIter, class RandomAccessIter, class RandomNumberGenerator>

inline RandomAccessIter random_sample(InputIter first, InputIter last, RandomAccessIter out_first,

RandomAccessIter out_last, RandomNumberGenerator& rand){

return random_sample(first,last,out_first,rand,out_last-out_first);

}

 

27. partition容器分割

用于重新分割排列容器的元素。返回值为最后的分界元素。

函数源代码:

template <class BidirectionalIterator, class Predicate>

BidirectionalIterator partition ( BidirectionalIterator first,

BidirectionalIterator last, Predicate pred )

{

while (true)

{

while (first!=last && pred(*first)) ++first;

if (first==last--) break;

while (first!=last && !pred(*last)) --last;

if (first==last) break;

swap (*first++,*last);

}

return first;

}

28. stable_partition容器稳定分割

也是重新分割排列容器内的元素,但是可以保持原有的顺序。实现的时候使用了临时缓冲区。

 

三、排序算法

1. sort排序

最坏情况下也为O(nlogn),较对排序稍快些。

函数原型:

template <class RandomAccessIterator>

void sort ( RandomAccessIterator first, RandomAccessIterator last );

 

template <class RandomAccessIterator, class Compare>

void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

2. stable_sort稳定排序

函数原型:

template <class RandomAccessIterator>

void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );

 

template <class RandomAccessIterator, class Compare>

void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,

Compare comp );

3.partial_sort 局部排序

仅对部分元素进行排序,通常挑选最小(或最大)的若干元素。内部使用堆排序实现,复杂度为O(nlogn).

函数原型:

template <class RandomAccessIterator>

void partial_sort ( RandomAccessIterator first, RandomAccessIterator middle,

RandomAccessIterator last );

 

template <class RandomAccessIterator, class Compare>

void partial_sort ( RandomAccessIterator first, RandomAccessIterator middle,

RandomAccessIterator last, Compare comp );

4. partial_sort_copy 局部排序复制

本质上是进行局部的堆排序,只是排序结果被放置到另一个区间范围中去。

函数原型:

template <class InputIterator, class RandomAccessIterator>

RandomAccessIterator

partial_sort_copy ( InputIterator first,InputIterator last,

RandomAccessIterator result_first,

RandomAccessIterator result_last );

 

template <class InputIterator, class RandomAccessIterator, class Compare>

RandomAccessIterator

partial_sort_copy ( InputIterator first,InputIterator last,

RandomAccessIterator result_first,

RandomAccessIterator result_last, Compare comp );

5.nth_element 第n个元素

算法仅排序第n个元素

函数原型:

template <class RandomAccessIterator>

void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,

RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>

void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,

RandomAccessIterator last, Compare comp );

四、二叉搜索

1. lower_bound 下确界

用于在有序区间中查找首个不小于某值的元素。采用的是折半查找的方式.

函数原型:

template <class ForwardIterator, class T>

ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,

const T& value );

 

template <class ForwardIterator, class T, class Compare>

ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,

const T& value, Compare comp );

函数源代码:

template <class ForwardIterator, class T>

ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value )

{

ForwardIterator it;

iterator_traits<ForwardIterator>::difference_type count, step;

count = distance(first,last);

while (count>0)

{

it = first; step=count/2; advance (it,step);

if (*it<value) // or: if (comp(*it,value)), for the comp version

{ first=++it; count-=step+1; }

else count=step;

}

return first;

}

2. upper_bound上确界

用于在有序区间中查找首个不大于某值的元素。采用的是折半查找的方式.

函数原型:

template <class ForwardIterator, class T>

ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,

const T& value );

 

template <class ForwardIterator, class T, class Compare>

ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,

const T& value, Compare comp );

函数源代码:

template <class ForwardIterator, class T>

ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last, const T& value )

{

ForwardIterator it;

iterator_traits<ForwardIterator>::difference_type count, step;

count = distance(first,last);

while (count>0)

{

it = first; step=count/2; advance (it,step);

if (!(value<*it)) // or: if (!comp(value,*it)), for the comp version

{ first=++it; count-=step+1; }

else count=step;

}

return first;

}

3. equal_range 等价区间

函数原型:

template <class ForwardIterator, class T>

pair<ForwardIterator,ForwardIterator>

equal_range ( ForwardIterator first, ForwardIterator last, const T& value );

 

template <class ForwardIterator, class T, class Compare>

pair<ForwardIterator,ForwardIterator>

equal_range ( ForwardIterator first, ForwardIterator last, const T& value,

Compare comp );

函数源代码:

template <class ForwardIterator, class T>

pair<ForwardIterator,ForwardIterator>

equal_range ( ForwardIterator first, ForwardIterator last, const T& value )

{

ForwardIterator it = lower_bound (first,last,value);

return make_pair ( it, upper_bound(it,last,value) );

}

 

4. binary_search

函数原型:

template <class ForwardIterator, class T>

bool binary_search ( ForwardIterator first, ForwardIterator last,

const T& value );

 

template <class ForwardIterator, class T, class Compare>

bool binary_search ( ForwardIterator first, ForwardIterator last,

const T& value, Compare comp );

函数源代码:

template <class ForwardIterator, class T>

bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value )

{

first = lower_bound(first,last,value);

return (first!=last && !(value<*first));

}

 

五、归并操作

1. merge 归并

函数原型:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result );

 

template <class InputIterator1, class InputIterator2,

class OutputIterator, class Compare>

OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result, Compare comp );

函数源代码:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result )

{

while (true) {

*result++ = (*first2<*first1)? *first2++ : *first1++;

if (first1==last1) return copy(first2,last2,result);

if (first2==last2) return copy(first1,last1,result);

}

}

 

 

2. inplace_merge 内部归并/原地归并

将一个序列内部两个升降同向的子序列进行排序合并。

该算法尽可能使用临时缓冲区进行归并排序,即使在临时缓冲区不可用的情形,算法的复杂度也为O(nlogn)

函数原型:

template <class BidirectionalIterator>

void inplace_merge ( BidirectionalIterator first, BidirectionalIterator middle,

BidirectionalIterator last );

 

template <class BidirectionalIterator, class Compare>

void inplace_merge ( BidirectionalIterator first, BidirectionalIterator middle,

BidirectionalIterator last, Compare comp );

3. includes 子集合

检测一个有序区间是否包含另一个有序区间,相当于一个集合包含关系判断。

函数原型:

template <class InputIterator1, class InputIterator2>

bool includes ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2 );

 

template <class InputIterator1, class InputIterator2, class Compare>

bool includes ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2, Compare comp );

函数源代码:

template <class InputIterator1, class InputIterator2>

bool includes ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2 )

{

while (first1!=last1)

{

if (*first2<*first1) break;

else if (*first1<*first2) ++first1;

else { ++first1; ++first2; }

if (first2==last2) return true;

}

return false;

}

4. set_union 集合求并

函数原型:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result );

 

template <class InputIterator1, class InputIterator2,

class OutputIterator, class Compare>

OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result, Compare comp );

函数源代码:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result )

{

while (true)

{

if (first1==last1) return copy(first2,last2,result);

if (first2==last2) return copy(first1,last1,result);

 

if (*first1<*first2) *result++ = *first1++;

else if (*first2<*first1) *result++ = *first2++;

else { *result++ = *first1++; first2++; }

}

}

5. set_intersection 集合求交

函数原型:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result );

 

template <class InputIterator1, class InputIterator2,

class OutputIterator, class Compare>

OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result, Compare comp );

函数源代码:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result )

{

while (first1!=last1 && first2!=last2)

{

if (*first1<*first2) ++first1;

else if (*first2<*first1) ++first2;

else { *result++ = *first1++; first2++; }

}

return result;

}

6. set_difference 集合求差

函数原型:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator set_difference ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result );

 

template <class InputIterator1, class InputIterator2,

class OutputIterator, class Compare>

OutputIterator set_difference ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result, Compare comp );

函数源代码:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator set_difference ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result )

{

while (first1!=last1 && first2!=last2)

{

if (*first1<*first2) *result++ = *first1++;

else if (*first2<*first1) first2++;

else { first1++; first2++; }

}

return copy(first1,last1,result);

}

7. set_symmetric_difference 集合求异

函数原型:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator

set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result );

 

template <class InputIterator1, class InputIterator2,

class OutputIterator, class Compare>

OutputIterator

set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result, Compare comp );

函数源代码:

template <class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator

set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

OutputIterator result )

{

while (true)

{

if (first1==last1) return copy(first2,last2,result);

if (first2==last2) return copy(first1,last1,result);

 

if (*first1<*first2) { *result++ = *first1++; }

else if (*first2<*first1) { *result++ = *first2++; }

else { first1++; first2++; }

}

}

 

六、堆操作

这儿有一个对堆操作的总结:http://www.cnblogs.com/xkfz007/archive/2012/08/25/2655620.html

1. push_heap 元素入堆

函数原型:

template <class RandomAccessIterator>

void push_heap ( RandomAccessIterator first, RandomAccessIterator last );

 

template <class RandomAccessIterator, class Compare>

void push_heap ( RandomAccessIterator first, RandomAccessIterator last,

Compare comp );

2. pop_heap 元素出堆

函数原型:

template <class RandomAccessIterator>

void pop_heap ( RandomAccessIterator first, RandomAccessIterator last );

 

template <class RandomAccessIterator, class Compare>

void pop_heap ( RandomAccessIterator first, RandomAccessIterator last,

Compare comp );

3. make_heap 创建堆

函数原型:

template <class RandomAccessIterator>

void make_heap ( RandomAccessIterator first, RandomAccessIterator last );

 

template <class RandomAccessIterator, class Compare>

void make_heap ( RandomAccessIterator first, RandomAccessIterator last,

Compare comp );

4. sort_heap 堆排序

函数原型:

template <class RandomAccessIterator>

void sort_heap ( RandomAccessIterator first, RandomAccessIterator last );

 

template <class RandomAccessIterator, class Compare>

void sort_heap ( RandomAccessIterator first, RandomAccessIterator last,

Compare comp );

七、最大/最小操作

1. min

函数原型:

template <class T> const T& min ( const T& a, const T& b );

template <class T, class Compare>

const T& min ( const T& a, const T& b, Compare comp );

函数源代码:

template <class T> const T& min ( const T& a, const T& b ) {

return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for the comp version

}

2. max

函数原型:

template <class T> const T& max ( const T& a, const T& b );

template <class T, class Compare>

const T& max ( const T& a, const T& b, Compare comp );

函数源代码:

template <class T> const T& max ( const T& a, const T& b ) {

return (a<b)?b:a; // or: return comp(a,b)?b:a; for the comp version

}

3. min_element 最小元素

函数原型:

template <class ForwardIterator>

ForwardIterator min_element ( ForwardIterator first, ForwardIterator last );

 

template <class ForwardIterator, class Compare>

ForwardIterator min_element ( ForwardIterator first, ForwardIterator last,

Compare comp );

函数源代码:

template <class ForwardIterator>

ForwardIterator min_element ( ForwardIterator first, ForwardIterator last )

{

ForwardIterator lowest = first;

if (first==last) return last;

while (++first!=last)

if (*first<*lowest) // or: if (comp(*first,*lowest)) for the comp version

lowest=first;

return lowest;

}

4. max_element 最大元素

函数原型:

template <class ForwardIterator>

ForwardIterator max_element ( ForwardIterator first, ForwardIterator last );

 

template <class ForwardIterator, class Compare>

ForwardIterator max_element ( ForwardIterator first, ForwardIterator last,

Compare comp );

函数源代码:

template <class ForwardIterator>

ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )

{

ForwardIterator largest = first;

if (first==last) return last;

while (++first!=last)

if (*largest<*first) // or: if (comp(*largest,*first)) for the comp version

largest=first;

return largest;

}

5. lexicographical_compare 字典比较

函数原型:

template <class InputIterator1, class InputIterator2>

bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2 );

 

template <class InputIterator1, class InputIterator2, class Compare>

bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

Compare comp );

函数源代码:

template <class InputIterator1, class InputIterator2>

bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2 )

{

while (first1!=last1)

{

if (first2==last2 || *first2<*first1) return false;

else if (*first1<*first2) return true;

first1++; first2++;

}

return (first2!=last2);

}

6. next_permutation 下一排列组合

对区间元素进行一次排列,使之字典顺序大于原来的排列

函数原型:

template <class BidirectionalIterator>

bool next_permutation (BidirectionalIterator first,

BidirectionalIterator last );

 

template <class BidirectionalIterator, class Compare>

bool next_permutation (BidirectionalIterator first,

BidirectionalIterator last, Compare comp);

 

7. prev_permutation 上一排列组合

对区间元素进行一次排列,使之字典顺序小于原来的排列

函数原型:

template <class BidirectionalIterator>

bool prev_permutation (BidirectionalIterator first,

BidirectionalIterator last );

 

template <class BidirectionalIterator, class Compare>

bool prev_permutation (BidirectionalIterator first,

BidirectionalIterator last, Compare comp);

 

八、数值计算

1. accumulate 元素求和

将区间元素进行累计求和

函数原型:

template <class InputIterator, class T>

T accumulate ( InputIterator first, InputIterator last, T init );

template <class InputIterator, class T, class BinaryOperation>

T accumulate ( InputIterator first, InputIterator last, T init,

BinaryOperation binary_op );

函数源代码:

template <class InputIterator, class T>

T accumulate ( InputIterator first, InputIterator last, T init )

{

while ( first!=last )

init = init + *first++; // or: init=binary_op(init,*first++) for the binary_op version

return init;

}

2. adjacent_difference 相邻元素求差

函数原型:

template <class InputIterator, class OutputIterator>

OutputIterator adjacent_difference ( InputIterator first, InputIterator last,

OutputIterator result );

 

template <class InputIterator, class OutputIterator, class BinaryOperation>

OutputIterator adjacent_difference ( InputIterator first, InputIterator last,

OutputIterator result, BinaryOperation binary_op );

 

函数源代码:

template <class InputIterator, class OutputIterator>

OutputIterator adjacent_difference ( InputIterator first, InputIterator last,

OutputIterator result )

{

iterator_traits<InputIterator>::value_type val,prev;

*result++ = prev = *first++;

while (first!=last) {

val = *first++;

*result++ = val - prev; // or: *result++ = binary_op(val,prev) for binary_op version

prev = val;

}

return result;

}

3. inner_product 两序列元素内积

函数原型:

template <class InputIterator1, class InputIterator2, class T>

T inner_product ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, T init );

 

template <class InputIterator1, class InputIterator2, class T,

class BinaryOperation1, class BinaryOperation2>

T inner_product ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, T init,

BinaryOperation1 binary_op1,

BinaryOperation2 binary_op2 );

函数源代码:

template <class InputIterator1, class InputIterator2, class T>

T inner_product ( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, T init )

{

while (first1!=last1)

init = init + (*first1++)*(*first2++);

// or: init=binary_op1(init,binary_op2(*first1++,*first2++))

// for the binary_op's version

return init;

}

4. partial_sum 部分元素求和

函数原型:

template <class InputIterator, class OutputIterator>

OutputIterator partial_sum ( InputIterator first, InputIterator last,

OutputIterator result );

 

template <class InputIterator, class OutputIterator, class BinaryOperation>

OutputIterator partial_sum ( InputIterator first, InputIterator last,

OutputIterator result, BinaryOperation binary_op );

函数源代码:

template <class InputIterator, class OutputIterator>

OutputIterator partial_sum ( InputIterator first, InputIterator last,

OutputIterator result )

{

iterator_traits<InputIterator>::value_type val;

*result++ = val = *first++;

while (first!=last)

*result++ = val = val + *first++;

// or: *result++ = val = binary_op(val,*first++) for binary_op version

return result;

}

posted @ 2016-08-03 13:22  melo11  阅读(265)  评论(0编辑  收藏  举报
TOP