A common STL lookup algorithm _c language

Source: Internet
Author: User

"Effective STL" in a sentence of advice, as far as possible to replace the handwritten loop algorithm, search for the loop traversal, where summarized commonly used STL lookup algorithm;

There are three kinds of search, that is, Dot line:
The point is to find the target as a single element;
Line is to find the target for the interval;
The face is to find the target for the set;

For each category lookup, the default comparison function is equal, in order to meet the richer requirements, the algorithm also provides a custom comparison function version;

Single element Lookup

Find () comparison condition is equal to lookup

Find () finds a single element from a given interval, defining:

Copy Code code as follows:

Template <class Inputiterator, class t>
Inputiterator Find (Inputiterator, inputiterator, const t& val);

example, look for 30 from Myvector:

Copy Code code as follows:

int myints[] = {10, 20, 30, 40};
Std::vector<int> Myvector (myints,myints+4);
it = Find (Myvector.begin (), Myvector.end (), 30);
if (it!= myvector.end ())
Std::cout << "Element found in Myvector:" << *it << ' \ n ';
Else
Std::cout << "Element not found in myvector\n";

Find_if () Custom comparison function

Std::find_if (): Finds the first element satisfying the comparison function from a given interval;
example, look for the first element that can be divisible by 30 from Myvector:

Copy Code code as follows:

BOOL Cmpfunction (int i) {
Return ((i%30) ==0);
}
it = std::find_if (Myvector.begin (), Myvector.end (), cmpfunction);
Std::cout << "A:" << *it <<std::endl;

COUNT () statistic element occurrences

Std::count (): The number of occurrences of an element in the statistical interval;
Std:count_if (): Custom comparison function version of Count ()

Search_n () query where individual elements recur

Search_n (): Find is used to query a single element, and Search_n is used to look for elements that are repeated n times in the interval;

Example: Query Myvector 30 consecutive occurrences of 2 times:

Copy Code code as follows:

int myints[]={10,20,30,30,20,10,10,20};
Std::vector<int> Myvector (myints,myints+8);
it = Std::search_n (Myvector.begin (), Myvector.end (), 2, 30);

Search_n () supports custom comparison functions;

Adjacent_find () Where duplicate elements appear in the query interval

Adjacent_find () The location of repeated elements in the query interval, which supports the custom comparison function;

Query element boundary in Lower_bound () ordered interval

Lower_bound () is used to find the first value in a sorted interval that is not less than the given element:
Example: Find the lower bound in container v that is not less than 20:

Copy Code code as follows:

int myints[] = {10,20,30,30,20,10,10,20};
Std::vector<int> V (myints,myints+8); 10 20 30 30 20 10 10 20
Std::sort (V.begin (), V.end ()); 10 10 10 20 20 20 30 30
Std::vector<int>::iterator Low,up;
Low=std::lower_bound (V.begin (), V.end (), 20);
Std::cout << "lower_bound at position" << (Low-v.begin ()) << ' \ n ';

A similar algorithm has upper_bound () to find the first value that is greater than the given element in the ordered interval;
There is also Equal_range () to find the upper and lower bounds of the ordered interval; (return Lower_bound () and Upper_bound ());

Binary search for Binary_search () ordered interval

Binary_search () is used in an ordered interval to find whether an element is in this interval, note, the return value of this algorithm is bool,
is not a subscript position, its internal algorithm logic and Lower_bound () similar, behavior is:

Copy Code code as follows:

Template <class ForwardIterator, class t>
BOOL Binary_search (ForwardIterator, ForwardIterator last, const t& val)
{
A-Std::lower_bound (First,last,val);
Return (First!=last &&!) ( Val<*first));
}

Example: Find out if 3 exists from ordered interval V:

Copy Code code as follows:

int myints[] = {1,2,3,4,5,4,3,2,1};
Std::vector<int> V (myints,myints+9); 1 2 3 4 5 4 3 2 1
Std::sort (V.begin (), V.end ());
if (Std::binary_search (V.begin (), V.end (), 3))
Std::cout << "found!\n"; else std::cout << "not found.\n";

Min_element () find the smallest element

Min_element () finds the minimum value in a given interval;

Copy Code code as follows:

int myints[] = {3,7,2,5,6,4,9};
Std::cout << "The smallest element is" << *std::min_element (myints,myints+7) << ' \ n ';

Similar algorithms are: max_element () to find the maximum value;

Interval Search search ()

Search () Find the first occurrence of the child interval

Find () is used to look up a single element, and search () is used to find a child interval;
Example: Find where the child interval [20,30] occurs from Myvector:

Copy Code code as follows:

int needle1[] = {20,30};
it = Std::search (Myvector.begin (), Myvector.end (), Needle1, needle1+2);
if (It!=myvector.end ())
Std::cout << "Needle1 found at position" << (It-myvector.begin ()) << ' \ n ';

Search supports custom comparison functions;
Example: query for each element in a given interval smaller than 1 of the target interval;

Copy Code code as follows:

BOOL Cmpfunction (int i, int j) {
return (i-j==1);
}
int myints[] = {1,2,3,4,5,1,2,3,4,5};
Std::vector<int> haystack (MYINTS,MYINTS+10);
int needle2[] = {1,2,3};
Using predicate comparison:
it = Std::search (Haystack.begin (), Haystack.end (), Needle2, needle2+3, cmpfunction);

Find_end () Find the last occurrence of the child interval

Search () is used to find the first occurrence of the child interval, and Find_end () is used to find the last occurrence of the child range:
Find_end () supports custom comparison functions;

Equal () to determine whether two intervals are equal

Equal () is used to determine whether two intervals are equal, and the algorithm supports a custom comparison function;

Mismatch () query two interval for the first time a different position;

Mismatch () query two intervals first appear in different positions, this algorithm also supports the custom comparison function;

Collection Lookup

Find_first_of find any element in the collection

Find_first_of () is used to find any element in a given set:
Example: Find where the a,b,c appears from haystack:

Copy Code code as follows:

int mychars[] = {' A ', ' B ', ' C ', ' A ', ' B ', ' C '};
Std::vector<char> haystack (mychars,mychars+6);
int needle[] = {' C ', ' B ', ' A '};
Using default comparison:
it = find_first_of (Haystack.begin (), Haystack.end (), needle, needle+3);

FIND_FIRST_OF supports custom comparison functions;

The above mentioned is the entire content of this article, I hope you can enjoy.

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.