Search for C ++ STL Algorithms

Source: Internet
Author: User


1. Search Algorithms
Adjacent_find (first, last );

Search for two consecutive equal elements in the interval [first, last) for the first time, and return the iterator pointing to the first element. For comparison between continuous elements, the default value is =.

Adjacent_find (first, last, Pred );

The purpose is as above, but the comparison between elements is done through the function Pred. The PRED accepts elements of the element type in the two containers and returns the bool value.

Function prototype:
Template <class forwarditerator>
Forwarditerator adjacent_find (forwarditerator first, forwarditerator last)
{
Forwarditerator next = first; ++ next;
If (first! = Last)
While (next! = Last)
If (* First ++ = * Next ++) // or: If (PRED (* First ++, * Next ++), for the PRED version
Return first;
Return last;
}

Example:
// Adjacent_find example
# Include <iostream>
# Include <algorithm>
# Include <vector>
Using namespace STD;

Bool myfunction (int I, Int J ){
Return (I = J );
}

Int main (){
Int myints [] = {10, 20, 30, 20, 10, 10 };
Vector <int> myvector (myints, myints + 8 );
Vector <int>: iterator it;

// Using default comparison:
It = adjacent_find (myvector. Begin (), myvector. End ());

If (it! = Myvector. End ())
Cout <"the first consecutive repeated elements are:" <* It <Endl;

// Using predicate comparison:
It = adjacent_find (++ it, myvector. End (), myfunction );

If (it! = Myvector. End ())
Cout <"the second consecutive repeated elements are:" <* It <Endl;

Return 0;
}
 

Output:
The first consecutive repeated elements are: 30
The second consecutive repeated elements are: 10

Find (first, last, value );
Find the element whose value is between the interval [first, last) and return the iterator type. If no value is found, return the end function prototype at the end of the iterator:
Template <class inputiterator, class T>
Inputiterator find (inputiterator first, inputiterator last, const T & value)
{
For (; first! = Last; first ++) if (* First = value) break;
Return first;
}
Example: // find example
# Include <iostream>
# Include <algorithm>
# Include <vector>
Using namespace STD;

Int main (){
Int myints [] = {10, 20, 30, 40 };
Int * P;

// Pointer to array element:
P = find (myints, myints + 4, 30 );
++ P;
Cout <"the element following 30 is" <* P <Endl;

Vector <int> myvector (myints, myints + 4 );
Vector <int>: iterator it;

// Iterator to vector element:
It = find (myvector. Begin (), myvector. End (), 30 );
++ It;
Cout <"the element following 30 is" <* It <Endl;

Return 0;
}
 

Output:
The element following 30 is 40
The element following 30 is 40

Find_if (first, last, Pred );
The first iterator in the return range [first, last) that enables the PRED function to return true elements; otherwise, the last iterator is returned. Note: PRED accepts a parameter function prototype:
Template <class inputiterator, class predicate>
Inputiterator find_if (inputiterator first, inputiterator last, predicate Pred)
{
For (; first! = Last; first ++) if (PRED (* First) break;
Return first;
}
Example: // find_if example
# Include <iostream>
# Include <algorithm>
# Include <vector>
Using namespace STD;

Bool isodd (int I ){
Return (I % 2) = 1 );
}

Int main (){
Vector <int> myvector;
Vector <int>: iterator it;

Myvector. push_back (10 );
Myvector. push_back (25 );
Myvector. push_back (40 );
Myvector. push_back (55 );

It = find_if (myvector. Begin (), myvector. End (), isodd );
Cout <"the first odd value is" <* It <Endl;

Return 0;
}
 

Output:
The first odd value is 25

 

Find_first_of (first1, last1, first2, last2 );
Find_first_of (first1, last1, first2, last2, Pred );
Returns an iterator so that any element in [first2, last2) appears in the interval [first1, last1) for the first time.
The default value is =. Of course, you can also define the PRED function (two parameters are accepted) and return the bool type function prototype:
Template <class forwarditerator1, class forwarditerator2>
Forwarditerator1 find_first_of (forwarditerator1 first1, forwarditerator1 last1,
Forwarditerator2 first2, forwarditerator2 last2)
{
For (; first1! = Last1; ++ first1)
For (forwarditerator2 it = first2; it! = Last2; ++ it)
If (* It = * first1) // or: If (COMP (* It, * First) for the PRED version
Return first1;
Return last1;
}
Example: // find_first_of example
# Include <iostream>
# Include <algorithm>
# Include <cctype>
# Include <vector>
Using namespace STD;

Bool comp_case_insensitive (char C1, char C2 ){
Return (tolower (C1) = tolower (C2 ));
}

Int main (){
Int mychars [] = {'A', 'B', 'C', 'A', 'B', 'C '};
Vector <char> myvector (mychars, mychars + 6 );
Vector <char >:: iterator it;

Int match [] = {'A', 'B', 'C '};

// Using default comparison:
It = find_first_of (myvector. Begin (), myvector. End (), match, match + 3 );

If (it! = Myvector. End ())
Cout <"first match is:" <* It <Endl;

// Using predicate comparison:
It = find_first_of (myvector. Begin (), myvector. End (),
Match, match + 3, comp_case_insensitive );

If (it! = Myvector. End ())
Cout <"first match is:" <* It <Endl;

Return 0;
}
 

Output:
First match is:
First match is:

 

Find_end (first1, last1, first2, last2 );
Find_end (first1, last1, first2, last2, Pred );
Returns an element iterator so that the last time [fiest2, last2) appears in the interval [first1, last1 ),
The default comparison is =. Of course, you can also write your own comparison function Pred, accept two parameters, and return the bool value function prototype:
Template <class forwarditerator1, class forwarditerator2>
Forwarditerator1 find_end (forwarditerator1 first1, forwarditerator1 last1,
Forwarditerator2 first2, forwarditerator2 last2)
{
Forwarditerator1 it1, limit, RET;
Forwarditerator2 it2;

Limit = first1; advance (limit, 1 + distance (first1, last1)-distance (first2, last2 ));
Ret = last1;

While (first1! = Limit)
{
It1 = first1; it2 = first2;
While (* it1 = * it2) // or: While (PRED (* it1, * it2) for the PRED version
{++ It1; ++ it2; If (it2 = last2) {ret = first1; break ;}}
++ First1;
}
Return ret;
}
Example: // find_end example
# Include <iostream>
# Include <algorithm>
# Include <vector>
Using namespace STD;

Bool myfunction (int I, Int J ){
Return (I = J );
}

Int main (){
Int myints [] = {1, 2, 3, 4, 5, 5, 5, 3, 3 };
Vector <int> myvector (myints, myints + 10 );
Vector <int>: iterator it;

Int mattings [] = {1, 2, 3 };

// Using default comparison:
It = find_end (myvector. Begin (), myvector. End (), mate8, mate8 + 3 );

If (it! = Myvector. End ())
Cout <"match1 last found at position" <int (it-myvector.begin () <Endl;

Int match2 [] = {4, 5, 1 };

// Using predicate comparison:
It = find_end (myvector. Begin (), myvector. End (), match2, match2 + 3, myfunction );

If (it! = Myvector. End ())
Cout <"match2 last found at position" <int (it-myvector.begin () <Endl;

Return 0;
}
 

Output:
Match found at position 5
Match found at position 3

Related Article

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.