The search algorithm of C + + STL

Source: Internet
Author: User

C++stl has several search algorithms, but their usage has many common areas:

1, except that the return value of Binary_search is bool (the lookup returns TRUE, otherwise returns false), all other lookup algorithm return values are an iterator (find the location of the iterator where the target was successfully returned, Otherwise returns the last position of the final element or the end of the container ())

2, the search algorithm will often use the iterator interval, note that the interval is closed after the open

3. If there are two intervals in all lookup functions, the first interval is the interval of the object being searched, and the second is the interval of the target object, if only one interval is the interval of the object being searched.

4, for the orderly lookup of the 3 functions, must be pre-ordered, otherwise it may be directly returned to find, do not be confused with the real non-existent element

Classification:

Find a single element find, find_if

Find the sub-range search, Search_n, Find_end, where find_end and search functions, just looking backwards

Search for a value in a sub-interval find_first_of

Searching algorithm for ordered interval binary_search,lower_bound,upper_bound, note that such lookups must be orderly, or return an error

1#include <iostream>2#include <algorithm>3 using namespacestd;4 intMain ()5 {6     //The find operation often uses an iterator interval, so be sure to note that it is closed before you can find it and return to the last position.7     //The input parameter is an iterator interval and the value to look for if the lookup successfully returns the iterator position of the first target value, looking for the last position where the failed return interval is located8     //Note: The iterator interval is open before closing, such as the iteration interval is [1,100], then the value of the subscript 1-99 is found, if the lookup fails, return 100 subscript corresponding to the iterator9     intnum[Ten]={0,1,2,8,4,7,5,7};Ten     //******* Find a single element find, find_if********************************// One     //_init Find (_init _first, _init _last, const _ty& _val) A     //Here's a little lazy, not using containers and iterators in the STL, but using arrays and pointers instead of -     int*p; -P=find (num,num+8,7); the     if((P-num) >7) -     { -cout<<"without this value"<<Endl; -     } +     Else -     { +cout<<"The subscript for the first 7 is"<<p-num<<Endl; A     } at  -P=find (num,num+8,3); -     if((P-num) >7) -     { -cout<<"without this value"<<Endl; -     } in     Else -     { tocout<<"The subscript for the first 3 is"<<p-num<<Endl; +     } -  the     //find_if need to use a function object in the binder and STL, so we don't write the program first. *  $ Panax Notoginseng     //****************** Find sub-range search, Search_n, find_end**********************// -     //_fwdit1 Search (_fwdit1 _first1, _fwdit1 _last1,_fwdit2 _first2, _fwdit2 _last2) the     intnum1[Ten]={2,8,4}; +P=search (num,num+8, num1,num1+3); A     if((P-num) >7) the     { +cout<<"without the number of this interval"<<Endl; -     } $     Else $     { -cout<<"the first num1 where num is the lower mark"<<p-num<<Endl; -     } the     //Find_end, like search, is just looking backwards. -P=find_end (num,num+8, num1,num1+3);Wuyi     if((P-num) >7) the     { -cout<<"without the number of this interval"<<Endl; Wu     } -     Else About     { $cout<<"the last num1 where num is the lower mark is"<<p-num<<Endl; -     } -  -     //Find_end, like search, is just looking backwards. A  +     //Search_n finding locations with the same n values the     //_fwdit1 search_n (_fwdit1 _first1, _fwdit1 _last1,_diff2 _count, const _ty& _val) -     intnum2[Ten]={1,6,6,6,5,6,6,6,6,3}; $P=search_n (num2,num2+Ten,4,6); the     if((P-NUM2) >Ten) the     { thecout<<"without the number of this interval"<<Endl; the     } -     Else in     { thecout<<"The first qualifying target is the one where num is subscript"<<p-num2<<Endl; the     } About  the     //****************** A value in the search sub-interval find_first_of**********************// the     //find_first_of the     //_fwdit1 find_first_of (_fwdit1 _first1, _fwdit1 _last1,_fwdit2 _first2, _fwdit2 _last2, _Pr _Pred) +     intfnum1[Ten]={1,5,6,4,8,6,1,4,6,5}; -     intfnum2[Ten]={7, the,Ten,5,4,6}; theP=find_first_of (fnum1,fnum1+Ten, fnum2,fnum2+Ten);Bayi     if((P-FNUM1) >Ten) the     { thecout<<"There are no matching values"<<Endl; -     } -     Else the     { thecout<<"the first target that meets the criteria is where the fnum1 subscript is"<<p-fnum1<<Endl; the     } the  -     //a search algorithm for ****************** ordered interval binary_search,lower_bound,upper_bound**********************// the     //Binary_search the     //bool Binary_search (_fwdit _first, _fwdit _last, const _ty& _val) the     //Note that the binary lookup return value is of type bool,94     //you can only tell that the element you are looking for is not in the interval and cannot return to the position, according to its principle, because he is a jumping search, unable to determine which is the first occurrence of the target the     intbnum[Ten]={1,2,3,4,6,8,Ten, the, +}; theCout<<binary_search (bnum,bnum+9,8); the     //Upper_bound finds the first value that is larger than the target and returns the iterator where it is located98     //_fwdit Upper_bound (_fwdit _first, _fwdit _last, const _ty& _val) AboutP=upper_bound (bnum,bnum+9,9); -     if((P-bnum) >9)101     {102cout<<"There are no matching values"<<Endl;103     }104     Else the     {106cout<<"the first target that meets the criteria is where the bnum subscript is"<<p-bnum<<Endl;107     }108     //Lower_bound finds the first value that is smaller than the target and returns the iterator where it is located109     return 0; the}

The search algorithm of C + + STL

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.