STL Standard Library of C + + learning notes (ii) algorithm header file algorithm

Source: Internet
Author: User

#include <algorithm>

The algorithm header file contains a large stack of template functions, the algorithm provided by the STL library, which can be considered to be largely independent of each function. The types of algorithms available are:

1) Adjacent_find//The first pair of equal adjacent elements in the detection interval

  Template<class fwit>

Fwit Adjacent_find (Fwdit first,fwdit last); If successful, return first+n,n satisfies * (first+n) = = * (first+n+1), if no equal adjacent elements are present, return last

Template<class FWIT,CALSS pred>

Fwit adjacent_find (fwit first,fwit last,pred PR); The user can customize the condition or function, as long as the PR (* (* (first+n), * (first+n+1)) is satisfied, return to the first+n;

2) Binary_search//test if there is a value in the ordered interval, return bool

template<class Fwit,class t>

BOOL Binary_search (Fwit first,fwit last,const t& val); Find if Val is present in the interval [first,last), there is a return of true, there is no return false

Template<class Fwit,class T,class pred>

BOOL Binary_search (fwit first,fwit last,const t& val,pred PR);

3) Copy//copying one sequence from beginning to the other

  Template<class Init,class outit>

OUTLT copy (InIt first,init last,outit x); Copy the elements within the interval [first,last] to the target range [x,x+ (Last-first))

4) Copy_backward//Copy a sequence from tail to head to another sequence

Template<class Bidit1,class bidit2>

BidIt2 Copy_backward (BidIt1 first,bidit1 last,bidit2 x); The interval [First,last] is copied between the interval [x (Last-first), x), and the Order of replication is: the element pointed to by the Last-1 iterator is copied to x-1 until the first point element is copied to X (Last-first)

5) Count//calculates the number of elements in the sequence that are equal to a specified value

  Template<class Init,class t>

TypeName Iterator_traits<init>::d ifference_type count (InIt first,init last,const t& val); Note the return type

6) count_if//To calculate the number of elements in the sequence that make the function object PR (* (first+n)) return True

  Template<class Init,class pred>

TypeName Iterator_traits<init>::d ifference_type count_if (InIt first,init last,pred PR);

7) Equal//compare two sequences for equality

  Template<class Init1,class init2>

bool Equal (INIT1 first1,init1 last1,init2 first2); Determines whether the interval [first1,last1] is equal to the interval [first2,first2+ (LAST1-FIRST1))

Template<class Init1,class Init2,class pred>

bool Equal (INIT1 first1,init1 last1,init2 first2,pred PR);

8) Lower_bound//Returns the position of the first greater than or equal value Val in a non-descending sequence 

Template<class Fwdit,class t>

Fwdit lower_bound (fwdit first,fwdit last,const t& val);

Template<class Fwdit,class T,class pred>

Fwdit lower_bound (fwdit first,fwdit last,const t& val,pred PR);

9) Upper_bound//Returns the position of the first greater than the value Val in a non-descending sequence [primary, last]

Template<class Fwdit,class t>

Fwdit upper_bound (fwdit first,fwdit last,const t& val);

Template<class Fwdit,class T,class pred>

Fwdit upper_bound (fwdit first,fwdit last,const t& val,pred PR);

Equal_range//In the sorted [First,last] search for Val, it returns a pair of iterators I and J, where I is thefirst position in which Val can be insertedwithout destroying the order (i.e., lower_bound), J is thelast position Val can insert without destroying the order (i.e. Upper_bound)

  Template<class Fwdit,class t>

Pair<fwdit,fwdit> Equal_range (fwdit first,fwdit last,const t& val);

Template<class Fwdit,class T,class pred>

Pair<fwdit,fwdit> Equal_range (fwdit first,fwdit last,const t& val,pred PR);

One) Fill//Assign a specific value to each element in the sequence

  Template<class Fwdit,class t>

void Fill (Fwdit first,fwdit last,const t& val);

Fill_n//assigns a specific value to the first n elements in the sequence

  Template<class Outit,class Size,class t>

void Fill_n (Outit first,size n,const t& val);

The first element in the Find//detection sequence that equals a specified value

  Template<class Init,class t>

Init find (init first,init last,const t& val);

Find_end//Search for the last sub-sequence in a sequence that matches another sequence

  Template<class Fwdit1,class fwdit2>

FwdIt1 find_end (FwdIt1 first1,fwdit1 last1,fwdit2 first2,fwdit2 last2);

  Template<class Fwdit1,class Fwdit2,class pred>

FwdIt1 find_end (FwdIt1 first1,fwdit1 last1,fwdit2 first2,fwdit2 last2,pred PR);

Find_first_of//To detect the first occurrence of any element in another sequence in a sequence

  Template<class Fwdit1,class fwdit2>

FwdIt1 find_first_of (FwdIt1 first1,fwdit1 last1,fwdit2 first2,fwdit2 last2);

  Template<class Fwdit1,class Fwdit2,class pred>

FwdIt1 find_first_of (FwdIt1 first1,fwdit1 last1,fwdit2 first2,fwdit2 last2,pred PR);

FIND_IF//detection sequence The first element that makes a PR (* (first+n)) ==true appear

  Template<class Init,class pred>

Init find_if (init first,init last,pred PR);

For_each//For each element in the sequence, call function object F, execute f (* (First+n))

  Template<class Init,class fun>

Fun For_each (InIt first,init last,fun f);

Generate//To use the Function object G, assign a value of g () to each element in the sequence

  Template<class Fwdit,class gen>

void Generate (Fwdit first,fwdit Last,gen g);

Generate_n// to use the Function object G to assign a value of G () to the first n elements in the sequence

  Template<class Outit,class Size,class gen>

void Generate_n (Outit first,size N,gen g);

Includes//detect whether an ordered sequence contains elements that are equal to each element in another sequence

  Template<class Init1,class init2>

BOOL Includes (InIt1 first1,init1 last1,init2 first2,init2 last2);

  Template<class Init1,class Init2,class pred>

BOOL Includes (InIt1 first1,init1 last1,init2 first2,init2 last2,pred PR);

Inplace_merge//combine two ordered intervals [first,middle] and [middle,lase] in the same sequence into an ordered interval

  Template<class bidit>

void Inplace_merge (BidIt first,bidit middle,bidit last);

Template<class Bidit,class pred>

void Inplace_merge (BidIt first,bidit middle,bidit last,pred PR);

Merge//combine the ordered intervals of two different sequences into a new ordered interval

  Template<class Init1,class Init2,class outit>

Outit merge (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x);

Template<class init1,class init2,class Outit,class pred>

Outit merge (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x,pred PR);

Iter_swap//Exchange values of *x and *y in different sequences

  Template<class Fwdit1,class fwdit2>

void Iter_swap (FwdIt1 x,fwdit2 y);

Lexicographical_compare//For comparison of two sequences in dictionary order, return bool

  Template<class Init1,class init2>

BOOL Lexicographical_compare (InIt1 first1,init1 last1,init2 first2,init2 last2);

Template<class Init1,class Init2,class pred>

BOOL Lexicographical_compare (InIt1 first1,init1 last1,init2 first2,init2 last2,pred PR);

Make_heap//rearrange the established [first,last] sequence in operator< order or by function object

  Template<class ranit>

void Make_heap (Ranit first,ranit last);

Template<class Ranit,class pred>

void Make_heap (Ranit first,ranit last,pred PR);

(+ ) Max//detect larger values in two elements

  Template<class t>

Const t& MAX (const t& x,const t& y);

Template<class T,class pred>

Const t& MAX (const t& x,const t& y,pred PR);

Max_element//maximum value in the detection sequence

  Template<class fwdit>

Fwdit max_element (Fwdit first,fwdit last);

Template<class Fwdit,class pred>

Fwdit max_element (fwdit first,fwdit last,pred PR);

Min//Detect small values in two elements

  Template<class t>

Const t& min (const t& x,const t& y);

Template<class T,class pred>

Const t& min (const t& x,const t& y,pred PR);

Mix_element//The minimum value in the detection sequence

  Template<class fwdit>

Fwdit min_element (Fwdit first,fwdit last);

Template<class Fwdit,class pred>

Fwdit min_element (fwdit first,fwdit last,pred PR);

Mismatch//detect the first unequal place in two sequences

  Template<class Init1,class init2>

Pair<init1,init2> mismatch (InIt1 first,init1 last,init2 x);

Template<class Init1,class Init2,class pred>

Pair<init1,init2> mismatch (InIt1 first,init1 last,init2 x,pred PR);

Next_permutation//Change the order of the sequence, return False when all elements in the sequence are already in ascending order

  Template<class bidit>

BOOL Next_permutation (BidIt First,bidit last);

Template<class Bidit,class pred>

BOOL Next_permutation (BidIt first,bidit last,pred PR);

Prev_permutation//Change the order of the sequence, return False when all elements in the sequence are already in descending order

  Template<class bidit>

BOOL Prev_permutation (BidIt First,bidit last);

Template<class Bidit,class pred>

BOOL Prev_permutation (BidIt first,bidit last,pred PR);

Nth_element//To place the element n in an ascending order, all n preceding elements should be less than it, and all the elements after n are greater than it, but there is no ordering requirement for those elements

  Template<class ranit>

void Nth_element (Ranit first,ranit nth,ranit last);

Template<class Ranit,class pred>

void Nth_element (ranit first,ranit nth,ranit last,pred PR);

Partition//Move all elements that make PR (x) True to the beginning of the sequence

  Template<class Bidit,class pred>

BidIt partition (BidIt first,bidit last,pred PR);

Partition_sort//the smallest n (middle==first+n) elements are arranged in ascending order and moved to the beginning of the sequence

  Template<class ranit>

BidIt Partition_sort (ranit first,ranit middle,ranit last);

Template<class Ranit,class pred>

BidIt Partition_sort (ranit first,ranit middle,ranit last,pred PR);

Partition_sort_copy//the smallest n (last==first+n) elements are arranged in ascending order and the sequence is copied to [First2,last2]

  Template<class Init,class ranit>

Ranit partition_sort_copy (InIt first1,init last1,ranit first2,ranit last2);

Template<class Init,class Ranit,class pred>

Ranit partition_sort_copy (InIt first1,init last1,ranit first2,ranit last2,pred PR);

(PNS) pop_heap//Remove the element with the largest value from the heap

  Template<class ranit>

void Pop_heap (Ranit first,ranit last);

Template<class Ranit,class pred>

void Pop_heap (Ranit first,ranit last,pred PR);

Push_heap//Add an element to a heap

  Template<class ranit>

void Push_heap (Ranit first,ranit last);

Template<class Ranit,class pred>

void Push_heap (Ranit first,ranit last,pred PR);

Random_shuffle//random rearrangement of elements in a sequence

  Template<class ranit>

void Random_shuffle (Ranit first,ranit last);

Template<class Ranit,class fun>

void Random_shuffle (Ranit first,ranit last,fun& f);

Remove//Remove all elements with a Val value, returning an iterator value pointing to the end of the result sequence

  Template<class Fwdit,class t>

Fwdit Remove (fwdit first,fwdit last,const t& val);

Remove_copy//Remove all elements with a Val value and deposit the result in X

  Template<class Init,class Outit,class t>

Outit remove_copy (InIt first,init last,outit x,const t& val);

REMOVE_COPY_IF//Remove all elements that make the PR (* (first+n)) True and deposit the result in X

  Template<class Init,class Outit,class pred>

Outit remove_copy_if (InIt first,init last,outit x,pred PR);

REMOVE_IF//Remove all elements that make the PR (* (first+n)) True

  Template<class Fwdit,class pred>

Fwdit remove_if (fwdit first,fwdit last,pred PR);

Replace//replaces the value of an element in a sequence with a value of val_old with Val_new

  Template<class Fwdit,class t>

void replace (Fwdit first,fwdit last,const t& val_old,const t& val_new);

Replace_copy//Replace the value of the element in the sequence with the value of val_old with val_new and deposit the result in X

Template<class Init,class Outit,class t>

Outit replace_copy (InIt first,init last,outit x,const t& val_old,const t& val_new);

REPLACE_COPY_IF//Replace the value of the element in the sequence that causes the PR (* (first+n)) to True to val_new and save the result in X

Template<class init,class outit,class Pred,class t>

Outit replace_copy (InIt first,init last,outit x,pred pr,const t& val_new);

REPLACE_IF//Replace the value of the element in the sequence that causes the PR (* (first+n)) to True to Val_new  

Template<class Fwdit,class Pred,class t>

void replace (Fwdit first,fwdit last,pred pr,const t& val_new);

Reverse//Reverse a sequence

  Template<class bidit>

void reverse (BidIt first,bidit last);

Reverse_copy//Reverses a sequence and deposits the result in X

  Template<class Bidit,class outit>

void Reverse_copy (BidIt first,bidit last,outit x);

Rotate//Reverses the sequence at position middle so that the order of the reversed sequence changes to [Middle,..., last-1,... first]

  Template<class fwdit>

void rotate (Fwdit first,fwdit middle,fwdit last);

Rotate_copy//Invert the sequence at position middle and deposit the result in X

  Template<class Fwdit,class outit>

void Rotate_copy (Fwdit first,fwdit middle,fwdit last,outit x);

Search//detect where another sequence first appears in the sequence

  Template<class Fwdit1,class fwdit2>

FWDIT1 Search (FwdIt1 first1,fwdit1 last 1,fwdit2 first2,fwdit2 last2);

Template<class Fwdit1,class Fwdit2,class pred>

FWDIT1 Search (FwdIt1 first1,fwdit1 last 1,fwdit2 first2,fwdit2 last2,pred PR);

Search_n//detection sequence where the first occurrence of a continuous n element value of Val, or a continuous n order PR (* (first+n)) ==pr (val) place

  Template<class Fwdit,class Size,class t>

Fwdit search_n (fwdit first,fwdit last,size n,const t& val);

Template<class fwdit,class size,class T,class pred>

Fwdit search_n (fwdit first,fwdit last,size n,const t& val,pred PR);

Set_difference//For two sequences, the result set is an iterator that contains all elements belonging to the first sequence but not the second sequence, and returns the end position of the result set

  Template<class Init1,class Init2,class outit>

Outit set_difference (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x);

Template<class init1,class init2,class Outit,class pred>

Outit set_difference (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x,pred PR);

) set_intersection//asks for the intersection of two sequences, returns the end position iterator of the result set

  Template<class Init1,class Init2,class outit>

Outit set_intersection (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x);

Template<class init1,class init2,class Outit,class pred>

Outit set_intersection (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x,pred PR);

) set_symmetric_difference//Find the difference set of two sequences, the result set is the difference set of the first sequence relative to the second and the difference set on the second relative to the first, which returns the end position iterator of the result set

  Template<class Init1,class Init2,class outit>

Outit set_symmetric_difference (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x);

Template<class init1,class init2,class Outit,class pred>

Outit set_symmetric_difference (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x,Pred PR);

) set_union//For two sequences of the set, returns the end position of the result set iterator

  Template<class Init1,class Init2,class outit>

Outit set_union (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x);

Template<class init1,class init2,class Outit,class pred>

Outit set_union (InIt1 first1,init1 last1,init2 first2,init2 last2,outit x,pred PR);

() sort//sorted by ascending or custom sort function pr () reflow sequence [First,last]

Template<class ranit>

void sort (Ranit first,ranit last);

Template<class Ranit,class pred>

void sort (ranit first,ranit last,pred PR);

) sort_heap//Sort the heap if the elements in the sequence are already a heap

Template<class ranit>

void Sort_heap (Ranit first,ranit last);

Template<class Ranit,class pred>

void Sort_heap (Ranit first,ranit last,pred PR);

(stable_partition)//The element of the specified sequence is divided into two sub-sequences according to the specified PR function, which satisfies the PR () function, that is, the return value is true as the first sequence [V.begin (), bound), and [Bound, V.end ()) As a second sequence

The difference from partition is that stable_partition does not change the relative position of the same class of elements

  Template<class Bidit,class pred>

BidIt stable_partition (BidIt first,bidit last,pred PR);

Stable_sort///// sorting functions in ascending order or a custom sort function pr () reorder sequence [first,last), the function guarantees that the original relative order of the equal elements remains unchanged after ordering, where equality, including the element value equality and the provided function to represent two elements equal

  Template<class bidit>

void Stable_sort (BidIt first,bidit last);

Template<class Bidit,class pred>

void Stable_sort (BidIt first,bidit last,pred PR);

min) Swap//exchange value of two elements

  Template<class t>

void swap (t& x,t& y);

Swap_ranges//Exchange two elements of the interval, return to the first position of the second container that does not have an interchange

  Template<class Fwdit1,class fwdit2>

FwdIt2 swap_ranges (FwdIt1 first,fwdit1 last,fwdit2 x);

Transform///First function, call UOP (* (first_n)) for each element in the sequence [First,last], assign the result to X, second function to sequence [First1,last1], [First2,last2] Each element in a

Bop (* (first1+n), * (First2+n)), assigns the result to X

  Template<class Init,class Outit,class unop>

Outit transform (InIt first,init last,outit x,unop uop);

Template<class init1,class init2,class Outit,class binop>

Outit transform (InIt1 first1,init1 last1,init2 first2,outit x,binop bop);

) Unique//deduplication function, removing adjacent repeating elements in a sequence, preserving only the first element in a repeating element

  Template<class fwdit>

Fwdit Unique (Fwdit first,fwdit last);

Template<class Fwdit,class pred>

Fwdit Unique (fwdit first,fwdit last,pred PR);

) unique_copy//de-duplication function to remove adjacent repeating elements in a sequence, preserving only the first element in a repeating element, and then performing a copy operation

  Template<class Init,class outit>

Outit unique_copy (InIt first,init last,outit x);

Template<class Init,class Outit,class pred>

Outit unique_copy (InIt first,init last,outit x,pred PR);

STL Standard Library of C + + learning notes (ii) algorithm header file algorithm

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.