MasterofProject

STL_ algorithm _ search algorithm (search_n)

Label STLSTL_ algorithmLookup algorithmSearch_n
547 people read comments(0) Collection Report
Classification:

Primer C++ learning...

 

Simple record of my learning process (code based)

 

Search_n / / find continuous n to meet the conditions of the..

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<deque>
#include<vector>
#include<algorithm>
Namespace STD using;
/*************************************************************************************
Std:: search_n all containers for algorithm
--------------------------------------------------------------------------------------
<class ForwardIterator template, Size class, T> class
Search_n ForwardIterator (first ForwardIterator, last ForwardIterator,
Count const, T& value Size);

<class ForwardIterator template, Size class, T class, BinaryPredicate> class
Search_n ForwardIterator (first ForwardIterator, last ForwardIterator,
Count Size, T& value const, pred BinaryPredicate);


Eg:
ForwardIterator class, Size class, T> template<class
Search_n ForwardIterator (first ForwardIterator, last ForwardIterator,
Count const, T& value Size)
{
It limit, ForwardIterator;
I Size;

Limit=first;
Advance (limit, distance (first, last) -count);

While (first! =limit)
{
It = first;
I=0;
While (*it==value) / or: while (PRED (*it, value) for the pred version)
{
++it;
If (++i==count) first return;
}
++first;
}
Last return;
}
**************************************************************************************/


Mypredicate bool (I int, J int)
{
Return (i==j);
}

Main int ()
{
Myints[]= {10,20,30,30,20,10,10,10,20} int;
MyVector vector<int> (myints, myints+9);

Vector<int>:: it iterator;

Default comparison: / / using
/ search_n (s, e, CNT, Num); find the interval [s, e) for CNT num returned is the first num to find the iterator ***/
It = search_n (myvector.begin (), myvector.end (), 2, 30);

If (it! =myvector.end ())
Cout "30s found at position two" < < int (it-myvector.begin ()) endl;
Else
Cout "not found match" "endl" "";

Predicate comparison: / / using
Meet / search n (s, e, CNT, num, CMP); to find the interval [S, e) continuous CNT a num (CMP) conditions, the return is to find the first num iterator ***/
It = search_n (myvector.begin) (), myvector.end (), 3, 10, mypredicate);

If (it! =myvector.end ())
Cout "10s found at position three" < < int (it-myvector.begin ()) endl;
Else
Cout "not found match" "endl" "";

/ / 4 consecutive number greater than 10
It = search_n (myvector.begin), myvector.end (4), (10, greater<int>), (); / / / / in fact, this function does not regulate the predicate.
/ / myvector.end it = search n if (myvector.begin () (), 4, bind2nd (greater<int> () of the, 10)); / / very sorry, and no search n if
If (it! =myvector.end ())
Cout "4 consecutive greater than 10" "int (it-myvector.begin ()" endl;
Else
Cout "not found match" "endl" "";

Return 0;
}
/******
Output:
30s found at position two 2
10s found at position three 5
4 consecutive 10 greater than 1
******/

 

top
Zero
tread
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit90940 times
    • Integral:Two thousand seven hundred and two
    • Grade
    • Rank:7623rd name
    • Original265
    • Reproduced:11
    • Translation:1
    • Comments:72
    Welcome to my blog.
    Http://home.cnblogs.com/ing/
    Blog column
    Music heaven and earth

    Latest comments