A summary of the various algorithms commonly used in the C++11 standard.

Source: Internet
Author: User

Many algorithms are defined in the C++11 standard, which allows us to operate a variety of containers and arrays conveniently, and it is important to note that these algorithms operate not as containers, but as iterators, and then manipulate the data in the container through iterators, and the algorithm itself does not care about the type of data stored in the container.

Here is my summary of common-sense algorithms, most of which are in algorithm.h, and some in numeric.h.

Here are some of our containers with these:

Vector<string> VEC;

Vector<int> VEC1;

Vec<string> vec2;

1.find ();

The function of the find () algorithm is to find a number in a specified sequence, contain three parameters, the first two parameters are iterators that represent the range of elements, and the third parameter is the value to look for.

Example: Fing (Vec.begin (), Vec.end (), Val); In Vec, look for the value Val, which can be a number, a string.

2.count ();

The function of the count () algorithm is to see the number of occurrences of a given value in a given sequence, as well as three parameters, the first two are iterators that represent the range of elements, and the third parameter is the value to look for.

Example: Count (Vec1.begin (), Vec1.end (), 2),//view 2 occurrences in VEC1.

3.accumulate ();//The function is in the numeric.h file

The purpose of the accumulate () function is to calculate the sum of the numbers in the specified range. Contains three parameters, the first two parameters are the sequence range of sums, and the third parameter is the initial value of sum.

Example: Accumulate (Vec1.begin (), Vec1.end (), 0),//calculates the sum of each number in the VEC1, and the initial value is 0;

4.equal ();

The function of the equal () algorithm is to determine whether two sequences hold the same value, the algorithm accepts three iterators, the first two iterators represent the range of elements in the sequence, and the third iterator represents the hand element of the second sequence. Returns true if it is equal, otherwise false;

Equal (Vec.begin (), Vec.end (), Vec2.begin ());//Compare the elements in the VEC and VEC2 are the same.

5.fill ();

The function of the fill () algorithm is to assign a value to all elements within a range, containing three parameters, the first two parameters accepting a pair of iterators representing a range, and the third iterator accepting a value.

Example: Fill (Vec.begin (), Vec.end (), "");//Set all elements in the VEC to null.

6.fill_n ();

The function of the Fill_n algorithm is to assign a given value to the specified element that begins with the element pointed to by the iterator. The first parameter accepts an iterator, the second element accepts a count value, and the third iterator accepts a value.

Example: Fill_n (Vec.begin (), Vec.size (), "");//Place all elements in the VEC empty.

Note : Here's an introduction to Back_inserter, an insert iterator, an iterator that adds elements to a container that is defined in iterator.h and called once per back_inserter, and the function invokes push_back once.

Example: Auto i = Back_inseter (VEC1);//Assign a value to add an element to the VEC

*i = 42;//VEC1 has an existing element with a value of 42

Here Back_inserter is commonly used to create an iterator to use as the destination for the algorithm.

Example: Fill_n (Back_inserter (VEC1), 10, 0);//Add 10 elements to the end of VEC1, and the element value is 0;

7.copy ();

The copy () algorithm copies the elements in the input range into the destination sequence. Includes three parameters, the first two represent an input range, and the third represents the starting position of the sequence.

Example: int a1[] = {0,1,2,3,4,5,6,7,8,9};

int a2[sizeof (A1)/sizeof (*A1)];

RET points to the position after the copy to the A2 element

Auto ret = copy (begin (A1), end (A1), a2);//Copy the contents of A1 to A2.

8.replace ();

The replace () algorithm is used to change all the elements in a given sequence to another value, which receives four parameters, the first two iterators, the range of input sequences, the third represents the value to be searched, and the fourth is the new value to be swapped.

Example: replace (Vec.begin (), Vec.end (), "", "Test");//Change the empty string in the VEC to test.

If we need to ensure that the value of the original sequence is constant, you can use the replace_copy () algorithm to make the original sequence unchanged and copy the changed sequence values to the new container.

Example: Replace_copy (Vec.begin (), Vec.end (), Back_inserter (VEC2), "", "Test"), or//To save the modified new sequence to VEC2, the original sequence value is unchanged.

9.sort ();

The function of the sort () algorithm is to rearrange the elements in the input sequence, which is to rearrange the sequence according to the dictionary sequence.

Example:vector<string> VEC = {' The ', ' quick ', ' red ', ' for ', ' jump ', ' over ', ' the ', ' slow ', ' red ', ' turtle '};

Sort (Vec.begin (), Vec.end ());

Results after execution: for jump over quick red slow the The turtle

10.unique ();

The purpose of the unique () algorithm is to rearrange the input sequence numbers and make each previous word appear only once, returning an iterator to a position after the non-repeating region.

such as 9 Vec,auto end_unique = Unique (Vec.begin (), Vec.end ());

Results after execution: for-jump over quick red slow the turtle the

This is if we need to get a sequence that does not repeat, then we just need to use erase (), i.e.

Vec.erase (End_unique, Vec.end ());

Results after execution: for jump over quick red slow the turtle

Note : In order to speak the following algorithm, here first to popularize the predicate, here the so-called predicate is not the language syntax predicate, but the function name passed to the algorithm.

Example: bool Isshort (const string &S1, const string &s2)

{return s1.size () < S2.size ();}

The preceding sort () algorithm is also capable of receiving predicates, such as sort (Vec.begin (), Vec.end (), isshort);

The meaning of the statement is to rearrange the sequence, but it is arranged in the order of the length of the string from small to large.

11.partiton ();

The function of the partition () algorithm is to partition the contents of the input container, which returns an iterator that returns the position after the last element that is the predicate is true. Accept three +

Parameters, the first two are the range iterators of the elements, and the latter one is the dividing standard.

Example: bool Bigger (const string &a)

{return a.size () > 5;}

Partition (Vec.begin (), Vec.end (), bigger),//The VEC is divided, the first part is a string of length greater than 5, and the latter part is a string with a length of less than or equal to 5.

Note : Let's talk about callable object lambda, format [capture list] (parameter list)->reture type{function Boby}

Where capture list is a list of local variables defined in a lambda function (usually empty), reture type, parameter list, function Boby, and other functions represent the return type, the argument list, function body. The example in 11 can be written as:

Partition (Vec.begin (), Vec.end (), [] (const string &a) {return a.size () >5;}); /can achieve the same effect as above

12.find_if ();

The function of the find_if () algorithm is to find the first element in the input sequence that satisfies the condition, and returns the first iterator that points to the element that satisfies the condition.

Example: Auto WC = find_if (Vec.begin (), Vec.end (), [SZ] (const string &a) {return a.size>=sz;}); /Where SZ is a local variable defined inside a function that uses find_if.

The appeal algorithm obtains the first iterator to an element with a length not less than SZ, and returns Vec.end () if not.

13.for_each ();

The main function of the for_each () algorithm is to print the output sequence. Receives three parameters, the first two is the range of the printed element, and the latter is a callable object.

Example: For_each (WC, Vec.end (), [] (const string &s) {cout << s << "";});

cout << Endl;

The function of the above program is to print the elements from WC to vec.end () in the output sequence.

A summary of the various algorithms commonly used in the C++11 standard.

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.