STL non-variable algorithm-STL Algorithm

Source: Internet
Author: User

Original: STL non-variable algorithm-STL Algorithm

MilkCu (http://blog.csdn.net/milkcu)

Address: http://blog.csdn.net/milkcu/article/details/21114613

Abstract: The C ++ STL standard template library plays an important role in the field of data structure and algorithm time, greatly improving the development efficiency. STL consists of containers, iterators, and algorithms. This article mainly describes the non-Variable Algorithms in STL algorithms. This article briefly introduces the use of related functions from the perspective of practice.

Introduction

C ++ STL's Non-mutating algorithms is a set of template functions that do not destroy function data, it is used for processing sequence data one by one, element searching, subsequence searching, statistics, and matching. It is basically used in various containers. In the following description, the default iterator range is [first, last), and The iterator has "++" iterations and "*" access operations.

One-by-one processing algorithm for_each Functions

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

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

#include 
 
  #include #include 
  
   using namespace std;void print(int x) {cout << x << " ";}int main(void) {vector
   
     v;for(int i = 0; i < 10; i++) {v.push_back(i * 2);}for_each(v.begin(), v.end(), print);return 0;}
   
  
 

Result output:

0 2 4 6 8 10 12 14 16 18

Element search algorithm find Function

This function is used to find elements that are equal to a value. If the element referred to by iterator I meets * I = value, return iterator I. The element that meets the condition is not found, and the return value is last. As long as the first element that meets the condition is found, it returns to the iterator position and does not continue searching.

The following example finds the first element with a value of 6 in the vector of the container, and prints the position of the element and its former element.

#include 
 
  #include #include 
  
   using namespace std;int main(void) {vector
   
     v;for(int i = 0; i < 10; i++) {v.push_back(i * 2);}vector
    
     ::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;}
    
   
  
 

Result output:

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

Find_if Function

This function is a version of the predicate judgment function of find. it searches for elements that meet the predicate judgment function.

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

#include 
 
  #include #include 
  
   using namespace std;int divBy3(int x) {return x % 3 ? 0 : 1;}int main(void) {vector
   
     v;for(int i = 1; i < 10; i++) {v.push_back(i * 2);}vector
    
     ::iterator iv = find_if(v.begin(), v.end(), divBy3);if(iv == v.end()) {cout << "None could be divided by 3 with no remaineder." << endl;} else {cout << *iv << " could be divided by 3 with no remainder." << endl;}return 0;}
    
   
  
 

Result output:

6 cocould be divided by 3 with no remainder.

Adjacent_find Function

This function is used to find adjacent element pairs that are equal or meet conditions. It has two prototypes, one for finding equal two continuous elements, and the other for finding adjacent element pairs that meet the conditions using binary predicates.

The following instance program is used to find elements with the same elements and parity in the container.

#include 
 
  #include #include 
  
   using namespace std;int parity_equal(int x, int y) {return (x - y) % 2 == 0 ? 1 : 0;}int main(void) {vector
   
     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
    
     ::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." << endl;cout << "They are " << *iv << " and ";iv++;cout << *iv << endl;}return 0;}
    
   
  
 

Output result:

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 certain range. It has two prototypes, one being equal, and the other being binary predicates. If the element is found, the iterator is returned. Otherwise, the last position is returned.

The following instance program is used to calculate the first position where elements in container v2 appear in container v.

#include 
 
  #include #include 
  
   using namespace std;int main(void) {vector
   
     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
    
      v2;v2.push_back(3);v2.push_back(3);v2.push_back(5);vector
     
      ::iterator iv = find_first_of(v.begin(), v.end(), v2.begin(), v2.end());cout << "The position of the first equal element is " << iv - v.begin() << endl;return 0;}
     
    
   
  
 

Output result:

The position of the first equal element is 3

Element statistics algorithm count function

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

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

#include 
 
  #include #include 
  
   using namespace std;int main(void) {vector
   
     v;for(int i = 0; i < 17; i++) {v.push_back(i % 6);}int num = count(v.begin(), v.end(), 5);cout << "The number of 5 is " << num << endl;return 0;}
   
  
 

Output result:

The number of 5 is 2

Count_if Function

This function uses a predicate to determine the number of elements that meet the conditions in the iterator range. There are two prototype options: direct return or reference return.

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

#include 
 
  #include #include 
  
   using namespace std;int greaterThan10(int x) {return x > 10 ? 1 : 0;}int main(void) {vector
   
     v;for(int i = 0; i < 17; i++) {v.push_back(i);}int num = count_if(v.begin(), v.end(), greaterThan10);cout << "The number of the figure that greater than 10 is " << num << endl;return 0;}
   
  
 

Output result:

The number of the figure that greater than 10 is 6

Sequential matching algorithm mismatch Function

This function is used to compare two sequences to find the location of the first unmatched element. It has two prototypes used, which are not equal and do not meet the condition of binary predicates.

This function also involves the use of pair.

The following instance program compares two integer containers and finds out the unmatched numbers.

#include 
 
  #include #include 
  
   using namespace std;int main(void) {vector
   
     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
    
     ::iterator, vector
     
      ::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;}
     
    
   
  
 

Output result:

The dismatching figure are 5 and 7

Equal Function

This function compares the elements of the two sequences one by one, returns true/false, and does not return the iterator value. It has two use prototypes, which are element equality and binary predicates judgment conditions.

The following instance program is used to compare whether the absolute values of the numbers in the two containers are equal.

#include 
 
  #include #include 
  
   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
   
     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;}
   
  
 

Output result:

The elements of v1 and v2 are equal in abosolute value.

Search functions

This function searches for a child sequence that matches the other sequence. It uses two prototypes, namely full match and binary predicate judgment. If the matching succeeds, the iterator value of the first element of the sub-sequence is returned.

The search function is similar to the find_first_of function, but not the same. Search looks for the same region. The region must be the same as the elements in the list and their sequence. find_first_of is an element, as long as this element is any one of the following lists.

The following instance program illustrates the differences between search and find_first_of.

#include 
 
  #include #include 
  
   int main(void) {vector
   
     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
    
     ::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;}
    
   
  
 

Output result:

Position of search: 2
Position of find_first_of: 1

Search_n Function

This function is used to search for subsequences in which a series of element values are given values. It has two prototypes that use equal values and satisfy the judgment conditions of predicates.

The following instance program shows the process of searching for three consecutive numbers 8.

#include 
 
  #include #include 
  
   using namespace std;int main(void) {vector
   
     v;v.push_back(3);v.push_back(8);v.push_back(8);v.push_back(8);v.push_back(4);vector
    
     ::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;}
    
   
  
 

Result output:

Three consecutive 8 is founded.

Find_end Function

This function is used to search for the Child sequence that matches the last sequence with the other sequence. The search function is similar in the opposite direction.

The following instance program shows the process of searching for the last sub-sequence matching v1 in container v.

#include 
 
  #include #include 
  
   using namespace std;int main(void) {vector
   
     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
    
     ::iterator iv = find_end(v.begin(), v.end(), v2.begin(), v2.end());if(iv != v.end()) {cout << "The position of last matching subsequence is " << iv - v.begin() << endl;}return 0;}
    
   
  
 

Output result:

The position of last matching subsequence is 3

Summary

This article mainly introduces the non-variable algorithm in the C ++ STL algorithm library, which is an algorithm that does not change operation data in principle, including:

One-by-one search algorithm: for_each
Element search algorithms: find, find_if, adjacent_find, find_first_of
Element statistics algorithm: count, count_if
Sequential matching algorithm: dismatch, equal
Subsequence search algorithms: search, search_n, find_end

These functions are included in the header file. All the Code provided in this article is compiled and run in VS2010.

Reference

[1] http://www.cplusplus.com/reference/algorithm/,-C ++ Reference;

[2] C ++ STL development technology guidance, ye Zhijun, people's post and telecommunications Publishing House.

(Full text)

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.