C ++ STL's Non-mutating algorithms is a set of template functions that do not destroy operation data, it is used for processing sequence data one by one, element search, subsequence search, statistics, and matching.
The search algorithm function searches for the Child sequence that matches the other sequence in a sequence. It has the following two prototypes. On the iterator range [first1, last1), find the subsequence of the iterator range [first2, last2) that exactly matches (or satisfies the binary predicate binary_pred, return the iterator value of the first element of the Subsequence in the [first1, last1) interval, or return last1 to indicate that no matching subsequence exists.
Function prototype:
Template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search (
ForwardIterator1 _ First1,
ForwardIterator1 _ Last1,
ForwardIterator2 _ First2,
ForwardIterator2 _ Last2
);
Template <class ForwardIterator1, class ForwardIterator2, class Pr>
ForwardIterator1 search (
ForwardIterator1 _ First1,
ForwardIterator1 _ Last1,
ForwardIterator2 _ First2,
ForwardIterator2 _ Last2
BinaryPredicate _ Comp
);
Example program:
Search for the vector container v1 = {5, 8, 1, 4} To see if it contains the sub-sequence container vector V2 = {8, 1}. Then, print the search result "v2 elements are included in v1, starting element: v1 [1]"
/*************************************** ****************************
* Copyright (C) Jerry Jiang
* File Name: search. cpp
* Author: Jerry Jiang
* Create Time: 2011-10-10 23:22:34
* Mail: jbiaojerry@gmail.com
* Blog: http://blog.csdn.net/jerryjbiao
* Description: 10 of C ++ STL algorithm series interpreted by simple programs
* Non-variable algorithm: search by subsequence
**************************************** **************************/
# Include <algorithm>
# Include <vector>
# Include <iostream>
Using namespace std;
Int main ()
{
Vector <int> v1;
V1.push _ back (5 );
V1.push _ back (8 );
V1.push _ back (1 );
V1.push _ back (4 );
Vector <int> v2;
V2.push _ back (8 );
V2.push _ back (1 );
Vector <int>: iterator iterLocation;
IterLocation = search (v1.begin (), v1.end (), v2.begin (), v2.end ());
If (iterLocation! = V1.end ())
{
Cout <"v2 elements are included in the v1 container. The starting element is"
<"V1 [" <iterLocation-v1.begin () <"]" <endl;
}
Else
{
Cout <"v2 elements are not included in the v1 container" <endl;
}
Return 0;
}
From: Jerry Jiang's program life