About the use of the Lower_bound () function

Source: Internet
Author: User

The Lower_bound () function needs to load the header file #include<algorithm>, whose basic purpose is to find the position of the first element in an ordered interval greater than or equal to a given value , where the collation can be represented by a two-tuple relationship.

Function Prototypes:
Template<class ForwardIterator, class type> forwarditerator lower_bound ( forwarditerator _First, ForwardIterator _last, const type& _val ); Template<class forwarditerator, Class Type, class Binarypredicate> forwarditerator lower_bound ( forwarditerator _first, forwarditerator _Last, Const type& _val, Binarypredicate _comp );

Incoming parameter description:
_first to find the starting position of the interval
_last to find the end position of the interval
_val given the value used to find
_comp a custom representation of a function object that is less than a relationship, and returns TRUE or false depending on whether an element satisfies a less than relationship

To illustrate:
1#include <iostream>2#include <vector>3#include <algorithm>4 using namespacestd;5vector<int>v;6 intMain ()7 {8      for(inti =1; I <4; i++)9V.push_back (2* i);//Note that at this point the elements in V are in order.Tenvector<int>::iterator it = Lower_bound (V.begin (), V.end (),3); Onecout << *it <<Endl; A     return 0; -}

The above example is for the container, note that the return is the nearest pointer to element 3 it, the output is the *it result of element 4, if I want to get the position rather than the specific elements should do? Here's a tip for pointer offset, just subtract the pointer from the starting position, and the code looks like this:

1#include <iostream>2#include <vector>3#include <algorithm>4 using namespacestd;5vector<int>v;6 intMain ()7 {8      for(inti =1; I <4; i++)9V.push_back (2* i);//Note that at this point the elements in V are in order.Ten     //Vector<int>::iterator it = Lower_bound (V.begin (), V.end (), 3); One     intpos = Lower_bound (V.begin (), V.end (),3)-V.begin (); Acout << pos<<Endl; -     return 0; -}

This time back to Pos is the location of the found element, subscript, the element found here should be 4 in the container subscript is 1, so the output POS result is 1. Applicable to the container, the same applies to the array:

1#include <iostream>2#include <algorithm>3 using namespacestd;4 intMain ()5 {6     inta[4] = {2,4,6,8};//Note that the elements in a are ordered in their own right now7     int* it = Lower_bound (a,a+4,3);8cout << *it<<Endl;9     return 0;Ten}

The return position only needs to lose the starting position of the array:

1#include <iostream>2#include <algorithm>3 using namespacestd;4 intMain ()5 {6     inta[4] = {2,4,6,8};//Note that the elements in a are ordered in their own right now7     //int * it = lower_bound (a,a+4,3);8     intpos = Lower_bound (A, A +4,3)-A;//a indicates the starting position of the array9cout <<pos<<Endl;Ten     return 0; One}

The result is the same as the container.

For the case of 4 parameters, the last parameter defines the object that represents the size of the function, the usual reverse order can be loaded header file #include <functional>, inside there is a greater<int> ( ) function to find the nearest position in reverse order. If the same element as above is 2 4 6 8, reverse order is 8 6 4 2, then the distance 3 is the most recent representation of the element with 3 is less than or equal to 3, the output is element 2, the code is as follows:

1#include <iostream>2#include <vector>3#include <algorithm>4#include <functional>5 using namespacestd;6vector<int>v;7 intMain ()8 {9      for(inti =4; i >0; i--)TenV.push_back (2* i);//Note that at this point the elements in V are in order. Onevector<int>::iterator it = Lower_bound (V.begin (), V.end (),3,greater<int>()); Acout << *it <<Endl; -     return 0; -}

Note that the ordered sequence to look for must be valid, and the sequence has already been sorted.





About the use of the Lower_bound () function

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.