Understanding of Lower_bound () Upper_bound () in C + + STL

Source: Internet
Author: User

C++stl's set and Multiset containers have their own lower_bound () function and the Upper_bound () function, which can also be used in other containers, such as array, vector, and so on.

It is consistent in thought when used, but slightly different in usage. I use vectors and multiset to illustrate these two containers.

First, Vector

The Lower_bound () function returns the first position that is not less than the key of a given element

The Upper_bound () function returns the position of the first key greater than the given element

Usage:

#include <bits/stdc++.h>using namespacestd;intmain () {vector<int>v; V.push_back (1); V.push_back (2); V.push_back (2); V.push_back (3); V.push_back (3); V.push_back ( +); V.push_back (0); V.push_back ( About); V.push_back ( -); Vector<int>:: Iterator it;  for(it = V.begin (); It! = V.end (); it++) {cout<<*it<<" "; } cout<<"\ n----------\ n";    Sort (V.begin (), V.end ());  for(it = V.begin (); It! = V.end (); it++) {cout<<*it<<" "; } cout<<"\ n----------\ n"; //the use of Lower_bound () and Upper_bound () must be in an orderly situation:
Note that the subscript is starting from 0.vector<int>:: Iterator lower,upper;
Lower= Lower_bound (V.begin (), V.end (),2); Upper= Upper_bound (V.begin (), V.end (),3); cout<<"Subscript for the position of the first element not less than 2"<< (Lower-v.begin ()) <<"\ n"; cout<<"The first subscript that is greater than the position of element 3"<< (Upper-v.begin ()) <<"\ n" ; return 0;}

Second,Multiset

The Lower_bound () function returns the first value that is not less than the key of a given element

The Upper_bound () function returns the first value that is greater than the given element key

Usage:

#include <iostream>#include<Set>using namespacestd;intMain () {multiset<int>Mymultiset; Multiset<int>:: Iterator Itlow,itup; //usage: The same must be done in an orderly manner   for(intI=1; i<8; i++) Mymultiset.insert (i*Ten);//Ten .Itlow = Mymultiset.lower_bound ( -);//^ The value at the location you are seekingItup = Mymultiset.upper_bound ( +);//^ The value at the location you are seekingmymultiset.erase (Itlow,itup); //Delete the interval element from 30 to 50, remaining tencout<<*itlow<<Endl; cout<<*itup<<Endl; cout<<"Mymultiset contains:";  for(multiset<int>::iterator It=mymultiset.begin (); It!=mymultiset.end (); ++it) cout<<' '<< *it; cout<<"\ n"; return 0;}

reference resources;

Http://www.cplusplus.com

Understanding of Lower_bound () Upper_bound () in C + + STL

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.