Stl_ algorithm _ Lookup algorithm (Lower_bound, Upper_bound, Equal_range)

Source: Internet
Author: User

C + + Primer learning.

。。

Simple recording of My Learning process (code-based)


Applicable for all containers (O (log (n))) ordered interval lookup algorithm


Lower_bound ()//Find the first conforming element, return position iterator


Upper_bound ()//Find the last conforming element. Return position iterator


Equal_range ()//Find a pair of iterators pair (<>,<>)

An associative container has an equivalent member function. Better performance

#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include < Algorithm>using namespace std;/****************************************************************************** Std::lower_bound all sort containers for algorithm-------------------------------------- ------------------------------------------------Template <class ForwardIterator, class t> ForwardIterator Lower_bound (ForwardIterator First, ForwardIterator last, const t& value); template &L T;class ForwardIterator, Class T, class compare> ForwardIterator lower_bound (forwarditerator first, ForwardIterator Last, const t& value, Compare comp);//eg:template <class ForwardIterator, class T&G  T  ForwardIterator Lower_bound (forwarditerator first, ForwardIterator last, const t& value) {ForwardIterator it; Iterator_traits<forwarditerator>::d ifference_type Count, step;  Count = distance (first,last);    while (count>0) {it = first; step=count/2; advance (it,step);  if (*it<value)//Or:if (Comp (*it,value)), for the comp version {first=++it; count-=step+1;  } else Count=step; } return first;} *************************************************************************************//************************                           Std::upper_bound All sort containers apply Algorithm--------------------------------------------------------------------------------------template <                                Class ForwardIterator, Class t> ForwardIterator upper_bound (forwarditerator first, ForwardIterator last, const t& value); template <class ForwardIterator, class T, Class compare> ForwardIterator u Pper_bound (ForwardIterator First, ForwardIterator last, const t& value, Compare comp );//eg:template <Class ForwardIterator, Class t> ForwardIterator upper_bound (forwarditerator first, ForwardIterator last, const T&AMP ;  Value) {ForwardIterator it;  Iterator_traits<forwarditerator&gt::d ifference_type count, step;  Count = distance (first,last);    while (count>0) {it = first; step=count/2; advance (it,step); if (! (  Value<*it)//Or:if (!comp (Value,*it)), for the comp version {first=++it; count-=step+1;  } else Count=step; } return first;} *************************************************************************************//************************                           Std::equal_range All sort containers apply Algorithm--------------------------------------------------------------------------------------template < Class ForwardIterator, Class t> pair<forwarditerator,forwarditerator> Equal_range (forwarditerator first, Fo Rwarditerator last, const t& Value); template <class ForwardIterator, class T, class compare> pair<forwarditerator,forwarditerator> Equal_r Ange (ForwardIterator first, ForwardIterator last, const t& value, Compare comp);//eg:template < Class ForwardIterator, Class t> pair<forwarditerator,forwarditerator> Equal_range (forwarditerator first, Fo  Rwarditerator last, const t& value) {ForwardIterator it = Lower_bound (First,last,value); Return Make_pair (it, Upper_bound (It,last,value));} /bool mygreater (int i,int j) {return (I&GT;J);}    int main () {int myints[] = {10,20,30,30,20,10,10,20};           Vector<int> V (myints,myints+8);    Ten to ten Vector<int>::iterator low,up;                Sort (V.begin (), V.end ());    Ten-ten-cout<< "ten-ten-30\n"; Low=lower_bound (V.begin (), V.end (), 20);  //          ^  up= Upper_bound (V.begin (), V.end (), 20);    ^ cout << "Lower_bound at position" << Int (Low-v.begin ()) + 1 << Endl;    cout << "upper_bound at position" << Int (Up-v.begin ()) + 1 << Endl;    cout << Endl; /**-------------------------------------------------------------------------------**/pair<vector<int>    ::iterator,vector<int>::iterator> bounds;                              Using default comparison://sort (V.begin (), V.end ());            Ten-Bounds=equal_range (V.begin (), V.end (), 20);    ^ ^ cout<< "Ten-ten-30\n";    cout << "bounds at positions" << Int (Bounds.first-v.begin ());    cout << "and" << Int (Bounds.second-v.begin ()) << Endl;                   Using "Mygreater" as Comp:sort (V.begin (), V.end (), mygreater); Ten Bounds=equal_range (v.bEgin (), V.end (), mygreater);    ^ ^ cout<<endl<< "-Ten <<endl;    cout << "bounds at positions" << Int (Bounds.first-v.begin ());    cout << "and" << Int (Bounds.second-v.begin ()) << Endl; return 0;} /******output:10 Ten-Lower_bound at position 4 Upper_bound at position 7 10 10 10 20 2 0 bounds at positions 3 and 6, bounds at positions 2 and 5*/

Stl_ algorithm _ Lookup algorithm (Lower_bound, Upper_bound, Equal_range)

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.