STL algorithm of C + + standard library (iii)

Source: Internet
Author: User

  • Algorithm header file:

    #include<algorithm>#include<numeric>
  • Algorithm Introduction: The STL algorithm uses the overlay mode rather than the placement mode, so the caller must ensure that there is sufficient space for the operation.
  • function Object Type declaration:
    • Unaryproc:op (Elem), no return value
    • Compfunc:op (ELEM1,ELEM2), returns TRUE or False
    • Unarypredicate:op (Elem), returns TRUE or False
    • Binarypredicate:op (Elem,value), returns TRUE or False

      Non-easier algorithms
  • Element count

    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)    
  • Minimum value and maximum value

    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)
  • Find element
    ```
    Find the first matching element
    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);

Find values for top n consecutive matches
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);

Find the first sub-range
ForwardIterator1 Search (ForwardIterator1 beg,forwarditerator1 end,forwarditerator2 Searchbeg,forwarditerator2 Searchend);
ForwardIterator1 Search (ForwardIterator1 beg,forwarditerator1 end,forwarditerator2 Searchbeg,forwarditerator2 Searchend,binarypredicate Op);

Find the last sub-range
ForwardIterator1 find_end (ForwardIterator1 beg,forwarditerator1 end,forwarditerator2 Searchbeg,forwarditerator2 Searchend);
ForwardIterator1 find_end (ForwardIterator1 beg,forwarditerator1 end,forwarditerator2 Searchbeg,forwarditerator2 Searchend,binarypredicate Op);

Find the place where some elements first appear
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);

Find two contiguous and equal elements
ForwardIterator adjacent_find (forwarditerator beg,forwarditerator end);
ForwardIterator adjacent_find (forwarditerator beg,forwarditerator end,binarypredicate Op);

- 区间的比较

Verifying equality
bool Equal (InputIterator1 beg,inputiterator1 end,inputiterator2 Cmpbeg);
bool Equal (InputIterator1 beg,inputiterator1,end,inputiterator2 cmpbeg,binarypredicate op);

Test the equivalence of the indeterminate order (the order of the data exists does not matter)
BOOL Is_permutation (ForwardIterator1 beg,forwarditerator1 End,forwarditerator2 Beg);
BOOL Id_permutation (ForwardIterator1 beg,forwarditerator1 end,forwarditerator2 beg,compfunc op);

Find the first time different
Pair

Check whether sorting
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);

Check if the heap is formed
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);

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

##更易型算法

Copies the element, returning the first non-overridden element of the target interval
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); Reverse

Moving elements
Outputiterator Move (inputiterator sourcebeg,inputiterator sourceend,outputiterator Destbeg);
BidirectionalIterator2 Move_backward (BidirectionalIterator1 sourcebeg,bidirectioniterator1 sourceend, Bidircectioniterator2,destend);//Reverse

Single sequence element processing and writing to the target range
Outputiterator transform (inputiterator sourcebeg,inputiterator sourceend,outputiterator destbeg,unaryfunc op);

Two sequence elements are combined and written to the target range
Outputiterator transform (InputIterator1 sourcebeg,inputiterator1 Sourceend,inputiterator2 sourcebeg,outputiterator Destbeg,binaryfunc op);

Element Interchange
ForwardIterator2 swap_ranges (ForwardIterator1 beg,forwarditerator1 end,forwarditerator2 beg2);

Element Assignment Value
void Fill (forwarditerator beg,forwarditerator end,const t& elem);
void Fill_n (ForwardIterator beg,size num,const t& elem);

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

- 移除型算法

Remove some elements within a sequence
ForwardIterator Remove (forwarditerator beg,forwarditerator end,const t& value);
ForwardIterator remove_if (ForwardIterator beg,forwarditerator end,unarypredicate op);

Remove some elements while copying
Outputiterator remove_copy (inputiterator sourcebeg,inputiterator sourceend,outputiterator destBeg,const T& elem) ;
Outputiterator remove_copy_if (inputiterator sourcebeg,inputiterator sourceend,outputiterator DestBeg, Unarypredicate op);

To remove a continuous repeating element
ForwardIterator Unique (forwarditerator beg,forwarditerator end);
ForwardIterator Unique (forwarditerator beg,forwarditerator end,binarypredicate op);

Remove duplicate elements during copy
Outputiterator unique_copy (inputiterator sourcebeg,inputiterator sourcebeg,outputiterator destBeg);
Outputiterator unique_copy (inputiterator sourcebeg,inputiterator sourceend,outputiterator destBeg,BinaryPredicate OP);

- 变序型算法

Reverse element Order
void reverse (bidirectionaliterator beg,bidiectionaliterator end);
void Reverse_copy (Bidirectionaliterator sourcebeg,bidirectionaliterator sourceend,outputiterator destEnd);

Re-shuffle the elements inside the container
void Random_shuffle (Randomaccessiterator beg,randomiterator end);

- 排序算法

Sort all the elements
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);

Local sort (sort elements beg to sortend)
void Partial_sort (Randomaccessiterator beg,randomaccessiterator sortend,randomaccessiterator end);
void Partial_sort (Randomaccessiterator beg,randomaccessiterator sortend,randomsccessiterator end,BinaryPredicate op );

Heap algorithm
void Make_heap (Randomaccessiterator beg,randomaccessiterator end);
void Make_heap (Randomaccessiterator beg,randomaccessiterator end,binarypredicate op);
void Push_heap (Randomaccessiterator beg,randomaccessiterator end); Container first push,heap and then Push_heap
void Push_heap (Randomaccessiterator beg,randomaccessiterator end,binarypredicate op);
void Pop_heap (Randomaccessiterator beg,randomaccessiterator end); Heap re-pop_heap, container re-pop
void Pop_heap (Randomaccessiterator beg,randomaccessiterator end,binarypredicate op);

- 已排序区间算法

//Find element
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);//Returns the position where the first exists
BOOL Upper_bound ( ForwardIterator beg,forwarditerator end,const t& value); Returns the last existing position
pair

Merging elements
Outputiterator merge (Inputiterator sourcebeg,inputiterator sourceend,inputiterator source2beg,inputiterator Source2end,outputiterator Destbeg); Merges two elements of a collection, including summarization, union, and processing of intersections
Outputiterator set_union (inputiterator source1beg,inputiterator source1end,inputiterator source2Beg,InputIterator Source2end,outputiterator Destbeg); Merges the union of two sorted collections
Outputiterator set_intersection (inputiterator source1beg,inputiterator source1end,inputiterator Source2Beg, Inputiterator source2end,outputiterator Destbeg); Merges the intersection of two sorted elements
Outputiterator set_difference (inputiterator source1beg,inputiterator source1end,inputiterator Source2Beg, Inputiterator source2end,outputiterator Destbeg); Combine the difference set of two sorted elements

- 数值算法

T accumulate (inputiterator beg,inputiterator end,t initvalue); Sum
T inner_product (InputIterator1 beg,inputiterator1 end,inputiterator2 beg2,t initvalue); Seeking Product
Outputiterator partial_sum (inputiterator sourcebeg,inputiterator sourceend,outputiterator destBeg); Converts a relative value to an absolute value
```

STL algorithm of C + + standard library (iii)

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.