10 of the C ++ STL algorithm series: search

Source: Internet
Author: User

 

 

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

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.