C++ STL相關的一些演算法

來源:互聯網
上載者:User

sort()基於快速排序:是不穩定排序
使用方法1:sort(v.begin(),v.end())
排序方式:從小到大
使用方法2:sort(v.begin(),v.end(),greater<int>())
排序方式:從大到小

複雜度:O(N log(N)) comparisons (both average and worst-case), where N is last - first

stable_sort()基於堆排序,是穩定排序
使用方法1: stable_sort(v.begin(),v.end())
排序方式:從小到大
使用方法2:stable_sort(v.begin(),v.end(),greater<int>())
排序方式:從大到小

複雜度:Stable_sort is an adaptive algorithm: it attempts to allocate a temporary memory buffer, and its run-time complexity depends on how much memory is available. Worst-case behavior (if no auxiliary memory is available) is N (log N)^2 comparisons, where N is last - first, and best case (if a large enough auxiliary memory buffer is available) is N (log N).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

尋找演算法:

 

find()尋找容器中第一個匹配的元素,基於線性尋找
使用方法:
1、InputIterator
find (InputIterator beg, InputIterator end, const T& value)
2、InputIterator
find_if (InputIterator beg, InputIterator end, UnaryPredicate op)
尋找失敗,返回end迭代器;

複雜度:Linear: at most last - first comparisons for equality.

 

lower_bound()

 

基於二分尋找

演算法的形式:

template <class ForwardIterator, class LessThanComparable>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
                            const LessThanComparable& value);

template <class ForwardIterator, class T, class StrictWeakOrdering>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
                            const T& value, StrictWeakOrdering comp);

演算法的描述:

Lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last) [1]. Specifically, it returns the first position where value could be inserted without violating the ordering. [2] The first version of lower_bound uses operator< for comparison, and the second uses the function object comp.

The first version of lower_bound returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), *j < value.

The second version of lower_bound returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), comp(*j, value) is true.

 

複雜度:

The number of comparisons is logarithmic: at most log(last - first) + 1.

 

注意:對於數組使用STL模板返回的迭代器,其實就是數組元素類型的指標。(其實迭代器就是指標的一般情況)

 

 

binary_search()  二分尋找

 

演算法的形式:

template <class ForwardIterator, class LessThanComparable>
bool binary_search(ForwardIterator first, ForwardIterator last,
                   const LessThanComparable& value);

template <class ForwardIterator, class T, class StrictWeakOrdering>
bool binary_search(ForwardIterator first, ForwardIterator last, const T& value,
                   StrictWeakOrdering comp);
演算法的描述:

 

Binary_search is a version of binary search: it attempts to find the element value in an ordered range [first, last) It returns true if an element that is equivalent to [1] value is present in [first, last) and false if no such element exists. [2] The first version of binary_search uses operator< for comparison, and the second uses the function object comp.

Specifically, the first version returns true if and only if there exists an iterator i in [first, last) such that *i < value and value < *i are both false. The second version returns true if and only if there exists an iterator i in [first, last) such that comp(*i, value) and comp(value, *i) are both false.

 

演算法的複雜度:

 

The number of comparisons is logarithmic: at most log(last - first) + 2.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 關於Heap的一些演算法:(從STL,模板實現來看,使用的priority_queueu就是使用了這些標準演算法)

 

1、make_heap

 

演算法形式:

template <class RandomAccessIterator>void make_heap(RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class StrictWeakOrdering>void make_heap(RandomAccessIterator first, RandomAccessIterator last,               StrictWeakOrdering comp);
作用:Make_heap turns the range [first, last) into a heap 。
2、sort_heap (本質上就是一個堆排序,而且是不穩定的)
演算法形式:
template <class RandomAccessIterator>void sort_heap(RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class StrictWeakOrdering>void sort_heap(RandomAccessIterator first, RandomAccessIterator last,               StrictWeakOrdering comp);
作用:Sort_heap turns a heap [1] [first, last) into a sorted range.
 Note that this is not a stable sort: the relative order of equivalent elements is not guaranteed to be preserved.

3、pop_heap

演算法形式:
template <class RandomAccessIterator>void pop_heap(RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class StrictWeakOrdering>inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last,                     StrictWeakOrdering comp);
作用:Pop_heap removes the largest element (that is, *first) from the heap [1] [first, last).
 這裡還有一個有趣的postcondition:*(last - 1) is the element that was removed from the heap。
4、push_heap
演算法形式:
template <class RandomAccessIterator>void push_heap(RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class StrictWeakOrdering>void push_heap(RandomAccessIterator first, RandomAccessIterator last,               StrictWeakOrdering comp);
作用:Push_heap adds an element to a heap [1]. 
It is assumed that [first, last - 1) is already a heap; the element to be added to the heap is *(last - 1). 
示範:
int main(){  int A[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };  make_heap(A, A + 9);  cout << "[A, A + 9)  = "; //注意:push一個元素之前,原來的元素必須是一個heap,所以這裡先調用make_heap。  copy(A, A + 9, ostream_iterator<int>(cout, " "));    push_heap(A, A + 10);  cout << endl << "[A, A + 10) = ";  copy(A, A + 10, ostream_iterator<int>(cout, " "));  cout << endl;}
5、is_heap
演算法形式:
template <class RandomAccessIterator>bool is_heap(RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class StrictWeakOrdering>inline bool is_heap(RandomAccessIterator first, RandomAccessIterator last,                    StrictWeakOrdering comp)
作用:Is_heap returns true if the range [first, last) is a heap [1], and false otherwise.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~··

集合運算:

1、set_union():對兩個已經排好序的容器元素對進行union操作,返回的結果也保持排序狀態,且是穩定的。當兩個容器中有相同的元素時,使用第一個容器中取,而不是第二個
返回max(m,n)個重複元素,線性複雜度
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 StrictWeakOrdering>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
                         InputIterator2 first2, InputIterator2 last2,
                         OutputIterator result,
                         StrictWeakOrdering comp);
如:
int A[] = {0, 1, 2, 3};
 int B[]={ 4, 5, 6, 7, 8, 9 };
 
 int Asize=sizeof(A) / sizeof(int);
 int Bsize=sizeof(B) / sizeof(int);
 
 int* p=NULL;//這裡不能用指標參數來接受
 set_union(A,A+Asize,B,B+Bsize,ostream_iterator<int>(cout," "));
2、set_itersection():描述同上,返回min(m,n)個重複元素
3、set_difference():描述同上,返回min(m-n,0)個重複元素
4、set_symmetric_difference():返回A-B、B-A的並集
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
對於set、multiset、map、multimap中的find成員函數複雜度為log,基於紅/黑樹狀結構。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.