Simple Program interpretation of C ++ STL algorithm series 11: search_n

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.

Search_n search for repeated element subsequences: whether a series of element values in the search sequence are subsequences of a given value. It has the following two function prototypes, respectively in the iterator range [first, last), returns the iterator of the first element of the subsequence, or last indicates the subsequence without repeated elements.

Function prototype:

template<class ForwardIterator1, class Diff2, class Type>   ForwardIterator1 search_n(      ForwardIterator1 _First1,       ForwardIterator1 _Last1,      Size2 _Count,       const Type& _Val   );template<class ForwardIterator1, class Size2, class Type, class BinaryPredicate>   ForwardIterator1 search_n(      ForwardIterator1 _First1,       ForwardIterator1 _Last1,      Size2 _Count,       const Type& _Val,      BinaryPredicate _Comp   ); 

Example program:

In the search vector container ivect = {, 4,}, three continuous elements are 8, without four continuous elements 8, and two times of three continuous elements are 16.

/*************************************** * **************************** Copyright (c) jerry Jiang * file name: search_n.cpp * Author: Jerry Jiang * Create Time: 2011-10-11 22:23:47 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao * description: simple programs interpret the eleven * Non-Variable Algorithms in the C ++ STL algorithm series: search_n ********************************** * *******************************/# include <algo Rithm >#include <vector >#include <iostream> bool twice (const int para1, const int para2) {return 2 * para1 = para2;} using namespace STD; int main () {vector <int> ivect; ivect. push_back (1); ivect. push_back (8); ivect. push_back (8); ivect. push_back (8); ivect. push_back (4); ivect. push_back (4); ivect. push_back (3); vector <int>: iterator ilocation; ilocation = search_n (ivect. begin (), ivect. end (), 3, 8); If (ilocation! = Ivect. end () {cout <"3 consecutive elements 8 are found in ivect" <Endl ;} else {cout <"No three consecutive elements 8 in ivect" <Endl;} ilocation = search_n (ivect. begin (), ivect. end (), 4, 8); If (ilocation! = Ivect. end () {cout <"four consecutive elements 8 are found in ivect" <Endl ;} else {cout <"no four continuous elements 8 in ivect" <Endl;} ilocation = search_n (ivect. begin (), ivect. end (), 2, 4); If (ilocation! = Ivect. end () {cout <"two consecutive elements 4 are found in ivect" <Endl ;} else {cout <"no two consecutive elements 4 in ivect" <Endl;} ilocation = search_n (ivect. begin (), ivect. end (), 3, 16, twice); If (ilocation! = Ivect. end () {cout <"in ivect, double the value of three consecutive elements to 16" <Endl ;} else {cout <"in ivect, there are no three consecutive elements, two times as 16" <Endl ;}return 0 ;}

**************************************** **************************************** **************************************** *******

C ++ classic bibliography index and resource download:Http://blog.csdn.net/jerryjbiao/article/details/7358796

**************************************** **************************************** **************************************** ********

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.