C + + non-variable algorithm-STL algorithm _c language

Source: Internet
Author: User
Tags abs

C + + STL Standard Template Library plays an important role in the field of data structure and algorithm, which greatly improves the development efficiency. The three major components of STL are container, iterator and algorithm, this paper mainly explains the invariant algorithm in STL algorithm. This paper briefly introduces the use of related functions from the perspective of practice.

The invariant algorithm of C + + STL (non-mutating algorithms) is a set of template functions that do not destroy function data, which is used to deal with sequence data, element lookup, subsequence Search, statistic and match, and can be used in all kinds of containers. In the following narrative, the iterator interval defaults to [primary, last], and the iterator has "+ +" iterations and "*" Access operations.

Processing algorithm one by one


For_each function
This function performs the operation defined by a single parameter function object for each element of the iterator interval.

The following instance program prints each element in the vector of the container.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
void print (int x) {
cout << x << "";
}
int main (void) {
Vector<int> v;
for (int i = 0; i < i++) {
V.push_back (i * 2);
}
For_each (V.begin (), V.end (), print);
return 0;
}

The result output is:

Copy Code code as follows:

0 2 4 6 8 10 12 14 16 18

Element Lookup algorithm

Find function
This function is used to find elements that are equal to a value. If the element referred to in iterator I satisfies *i = = value, the iterator I is returned. The element that satisfies the condition was not found and returned last. The iterator position is returned as soon as the first element that satisfies the condition is found, and the lookup is no longer continued.

The following example program finds the element in the container vector with the first value of 6, printing the position of the element and its previous element.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int main (void) {
Vector<int> v;
for (int i = 0; i < i++) {
V.push_back (i * 2);
}
Vector<int>::iterator IV = FIND (V.begin (), V.end (), 6);
if (iv = = V.end ()) {
cout << "Find nothing." << Endl;
} else {
cout << "The postion of" << *iv << "is" << iv-v.begin () << Endl;
cout << "The previous element of it is" << * (--IV) << Endl;
}
return 0;
}

The result output is:

Copy Code code as follows:

The postion of 6 is 3
The previous element of it is 4

find_if function

This function is a predicate judgment version of find that finds elements that satisfy the predicate judgment function.

The following instance program looks for the first element in the container vector that is divisible by 3.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int divBy3 (int x) {
return x 3? 0:1;
}
int main (void) {
Vector<int> v;
for (int i = 1; i < i++) {
V.push_back (i * 2);
}
Vector<int>::iterator IV = FIND_IF (V.begin (), V.end (), divBy3);
if (iv = = V.end ()) {
cout << "None could is divided by 3 with no remaineder." << Endl;
} else {
cout << *iv << "Could is divided by 3 with no remainder." << Endl;
}
return 0;
}

The result output is:

Copy Code code as follows:

6 could is divided by 3 with no remainder.

Adjacent_find function

This function is used to find the adjacent element pairs that are equal or satisfy the condition. It has two prototypes, one for two consecutive elements to find equality, the other for a two-yuan predicate to find the neighboring element pairs that satisfy the condition.

The following instance program is used to look for equal elements and parity elements in the container.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int parity_equal (int x, int y) {
return (x-y)% 2 = 0? 1:0;
}
int main (void) {
Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (3);
V.push_back (5);
V.push_back (5);
V.push_back (7);
Vector<int>::iterator IV = Adjacent_find (V.begin (), V.end ());
if (iv!= v.end ()) {
cout << "There are two equal elements." << Endl;
cout << "It is" << *iv << Endl;
}
IV = Adjacent_find (V.begin (), V.end (), parity_equal);
if (iv!= v.end ()) {
cout << "There are two parity euqal." << elements;
cout << "They are" << *iv << "and";
iv++;
cout << *iv << Endl;
}
return 0;
}

The output results are:

Copy Code code as follows:

There are two equal elements.
It is 5
There are two parity euqal elements.
They are 3 and 5

find_first_of function

This function is used to find elements within a range. It has two use prototypes, one is equal, the other is two-yuan predicate judgments. element, the iterator is returned or the end position is returned.

The following instance program is used to calculate the first position of the element in container v2 that is coincident in container v.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int main (void) {
Vector<int> v;
V.push_back (1);
V.push_back (2);
V.push_back (2);
V.push_back (3);
V.push_back (5);
V.push_back (5);
V.push_back (7);
Vector<int> v2;
V2.push_back (3);
V2.push_back (3);
V2.push_back (5);
Vector<int>::iterator IV = find_first_of (V.begin (), V.end (), V2.begin (), V2.end ());
cout << "The position of the" the "the" the "the" the "equal element is" << iv-v.begin () << Endl;
return 0;
}

The output results are:

Copy Code code as follows:

The position of the equal element is 3

Element statistics algorithm

Count function
This function is used to calculate the number of occurrences of a given value in a container. It has two use prototypes, the difference being whether the count is returned directly or by reference.

The following instance program calculates the number of occurrences of 5 in the container and returns directly.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int main (void) {
Vector<int> v;
for (int i = 0; i < i++) {
V.push_back (i% 6);
}
int num = count (V.begin (), V.end (), 5);
cout << "The number of 5 is" << num << Endl;
return 0;
}

The output results are:

Copy Code code as follows:

The number of 5 is 2

count_if function

This function uses predicate judgment functions to count the number of elements in the iterator interval that meet the conditions. It has two use prototypes, the difference being whether the count is returned directly or the reference returns.

The following instance program counts the number of occurrences of a number greater than 10 in the container and returns directly.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int greaterThan10 (int x) {
return x > 10? 1:0;
}
int main (void) {
Vector<int> v;
for (int i = 0; i < i++) {
V.push_back (i);
}
int num = count_if (V.begin (), V.end (), GREATERTHAN10);
cout << "The number of the figure that greater than are" << num << Endl;
return 0;
}

The output results are:

Copy Code code as follows:

The number of the figure that greater than is 6

Sequence Matching algorithm

Mismatch function
This function is used to compare two sequences to find the first mismatched element position. It has two use prototypes, unequal and not satisfying two-yuan predicate conditions.

The function also involves the use of pair.

The following instance program compares two integral containers and finds mismatched numbers.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int main (void) {
Vector<int> v1, v2;
V1.push_back (3);
V1.push_back (5);
V1.push_back (5);
V2.push_back (3);
V2.push_back (5);
V2.push_back (7);
Pair<vector<int>::iterator, vector<int>::iterator> result = Mismatch (V1.begin (), V1.end (), V2.begin ());
if (Result.first = = V1.end () && Result.second = = V2.end ()) {
cout << "V1 is same as v2." << Endl;
} else {
cout << "The dismatching figure are"
<< * (Result.first) << "and"
<< * (Result.second) << Endl;
}
return 0;
}

The output results are:

Copy Code code as follows:

The dismatching figure are 5 and 7

Equal function

The function compares the elements of two sequences to be equal, the return value is True/false, and the iterator value is not returned. It has two use prototypes, namely element equality and two-yuan predicate judgment condition.

The following instance program is used to compare the equality of numeric values in two containers.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int absequal (int x, int y) {
return (x = = ABS (y) | | y = = ABS (x))? 1:0;
return ABS (x) = = ABS (y)? 1:0;
}
int main (void) {
Vector<int> v1, v2;
V1.push_back (3);
V1.push_back (5);
V1.push_back (5);
V2.push_back (3);
V2.push_back (5);
V2.push_back (-5);
if (Equal (V1.begin (), V1.end (), V2.begin (), absequal)) {
cout << "The Elements of V1 and V2 are equal in Abosolute value." << Endl;
} else {
cout << "The Elements of V1 and V2 are not equal in Abosolute value." << Endl;
}
return 0;
}

The output results are:

Copy Code code as follows:

The elements of V1 and V2 are equal in Abosolute value.

Sub-sequence Search algorithm

Search function

The function searches a sequence for a subsequence that matches another sequence. It has two prototypes, which are exact matches and two-yuan predicate judgments. A successful match returns the iterator value of the first element of the subsequence.

The search function resembles the find_first_of function, but not the same. Search is for the same area, which requires the same area as the elements and their order of the following list; Find_first_of is looking for an element, as long as it is any one of the following lists.

The following example program illustrates the difference between search and find_first_of.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
int main (void) {
Vector<int> v1, v2;
V1.push_back (1);
V1.push_back (4);
V1.push_back (2);
V1.push_back (3);
V1.push_back (4);
V2.push_back (2);
V2.push_back (3);
V2.push_back (4);
Vector<int>::iterator Ivsearch, Ivfind;
Ivsearch = Search (V1.begin (), V1.end (), V2.begin (), V2.end ());
Ivfind = find_first_of (V1.begin (), V1.end (), V2.begin (), V2.end ());
cout << "Position of Search:" << ivsearch-v1.begin () << Endl;
cout << "Position of find_first_of:" << ivfind-v1.begin () << Endl;
return 0;
}

The output results are:

Copy Code code as follows:

Position of Search:2
Position of Find_first_of:1

Search_n function

This function is used to search for a sequence of sequences in which a series of element values are a sequence of a given value. It has two use prototypes, which are equal values and satisfy predicate conditions.

The following example program shows the process of looking for 3 consecutive numbers 8.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace Std;
int main (void) {
Vector<int> v;
V.push_back (3);
V.push_back (8);
V.push_back (8);
V.push_back (8);
V.push_back (4);
Vector<int>::iterator IV = Search_n (V.begin (), V.end (), 3, 8);
if (iv = = V.end ()) {
cout << "There are no three consecutive 8." << Endl;
} else {
cout << "Three consecutive 8 is founded." << Endl;
}
return 0;
}

The result output is:

Copy Code code as follows:

Three consecutive 8 is founded.

Find_end function
This function is used to search a sequence for the last subsequence that matches another sequence. The search function is similar, in opposite directions.

The following instance program shows the process of searching for the last substring in container v to match the V1.

Copy Code code as follows:

#include <iostream>
#include <algorithm>
#include < Vector>
using namespace std;
int main (void) {
    vector<int> V, v2;
    v.push_back (1);
    v.push_back (3);
    v.push_back (5);
    v.push_back (3);
    v.push_back (5);
    v.push_back (7);
    v2.push_back (3);
    v2.push_back (5);
    vector<int>::iterator IV = Find_end (V.begin (), V.end (), V2.begin (), V2.end ());
    if (iv!= v.end ()) {
        cout << The position of matching subsequence is "<< iv-v.begin () << Endl;
    }
    return 0;
}

The output results are:

Copy Code code as follows:

The position of matching subsequence is 3

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.