C ++ STL-Related Algorithms

Source: Internet
Author: User

Sort () based on quick sorting: unstable sorting
Method 1: Sort (V. Begin (), V. End ())
Sorting method: from small to large
Method 2: Sort (V. Begin (), V. End (), greater <int> ())
Sorting method: from large to small

Complexity: O (n log (N) comparisons (both average and worst-case), whereNIsLast-first

Stable_sort () is a stable sorting method based on heap sorting.
Method 1: stable_sort (V. Begin (), V. End ())
Sorting method: from small to large
Method 2: stable_sort (V. Begin (), V. End (), greater <int> ())
Sorting method: from large to small

Complexity: stable_sort isAdaptiveAlgorithm: 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) isN (log n) ^ 2Comparisons, whereNIsLast-first, And best case (if a large enough auxiliary memory buffer is available) isN (log n).

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

 

Search Algorithm:

 

Find () finds the First Matching Element in the container based on linear search
Usage:
1. inputiterator
Find (inputiterator beg, inputiterator end, const T & value)
2. inputiterator
Find_if (inputiterator beg, inputiterator end, unarypredicate OP)
An error occurred while searching. The end iterator is returned;

Complexity: Linear: at mostLast-firstComparisons for equality.

 

Lower_bound ()

 

Binary Search

Algorithm format:

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

Algorithm Description:

Lower_bound is a version of Binary Search: it attempts to find the elementValueIn an ordered Range[First, last)[1]. Specifically, it returns the first position whereValueCocould be inserted without violating the ordering. [2] the first versionLower_boundUsesOperator <For comparison, and the second uses the function objectComp.

The first versionLower_boundReturns the furthermost iteratorIIn[First, last)Such that, for every iteratorJIn[First, I),* J <Value.

The second versionLower_boundReturns the furthermost iteratorIIn[First, last)Such that, for every iteratorJIn[First, I),Comp (* j, value)IsTrue.

 

Complexity:

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

 

Note: For arrays, The iterator returned by using the STL template is actually a pointer of the array element type. (In fact, The iterator is generally a pointer)

 

 

Binary_search () binary search

 

Algorithm format:

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 );
Algorithm Description:

 

Binary_search is a version of Binary Search: it attempts to find the elementValueIn an ordered Range[First, last)It returnsTrueIf an element that is equivalent to [1]ValueIs present in[First, last)AndFalseIf no such element exists. [2] the first versionBinary_searchUsesOperator <For comparison, and the second uses the function objectComp.

Specifically, the first version returnsTrueIf and only if there exists an iteratorIIn[First, last)Such that* I <ValueAndValue <* IAre bothFalse. The second version returnsTrueIf and only if there exists an iteratorIIn[First, last)Such thatComp (* I, value)AndComp (value, * I)Are bothFalse.

 

Algorithm complexity:

 

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

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

 

Some heap algorithms: (based on STL and template implementation, the priority_queueu uses these standard algorithms)

 

1. make_heap

 

Algorithm format:

template <class RandomAccessIterator>void make_heap(RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class StrictWeakOrdering>void make_heap(RandomAccessIterator first, RandomAccessIterator last,               StrictWeakOrdering comp);
Purpose: make_heap turns the range[First, last)Into a heap.
2. sort_heap (in essence, it is a heap sorting and unstable)
Algorithm format:
template <class RandomAccessIterator>void sort_heap(RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class StrictWeakOrdering>void sort_heap(RandomAccessIterator first, RandomAccessIterator last,               StrictWeakOrdering comp);
Purpose: 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

Algorithm format:
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);
Role: pop_heap removes the largest element (that is,* First) From the heap [1][First, last).
There is also an interesting postcondition: * (last-1) is the element that was removed from the heap.
4、push_heap
Algorithm format:
template <class RandomAccessIterator>void push_heap(RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class StrictWeakOrdering>void push_heap(RandomAccessIterator first, RandomAccessIterator last,               StrictWeakOrdering comp);
Role: 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). 
Demo:
Int main () {int A [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; make_heap (, A + 9); cout <"[A, A + 9) ="; // Note: Before pushing an element, the original element must be a heap, therefore, make_heap is called first. Copy (A, A + 9, ostream_iterator <int> (cout, ""); push_heap (A, A + 10); cout <Endl <"[, A + 10) = "; copy (A, A + 10, ostream_iterator <int> (cout," "); cout <Endl ;}
5、is_heap
Algorithm format:
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)
Role: is_heap returnsTrueIf the range[First, last)Is a heap [1], andFalseOtherwise.

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

Set operation:

1. set_union (): Perform the union operation on two sorted container element pairs. The returned results are also sorted and stable. When two containers have the same elements, use the first container instead of the second one.
Returns max (m, n) repeated elements, linear complexity
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 );
For example:
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; // pointer parameters cannot be used here
Set_union (A, A + asize, B, B + bsize, ostream_iterator <int> (cout ,""));
2. set_itersection (): The description is the same as above. duplicate elements in min (m, n) are returned.
3. set_difference (): The description is the same as above. duplicate elements in min (m-N, 0) are returned.
4. set_effecric_difference (): returns the Union of A-B and B-A.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
The complexity of the find member functions in set, Multiset, map, and multimap is log, which is based on the red/black tree.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.