STL:map中的lower_bound和upper_bound

今天在做leetcode的Longest Increasing Subsequence题目时,需要用到二分查找,于是翻看了《STL源码剖析》这本书,发现map里面有lower_bound和upper_bound这两个函数。用法如下:

map<int,int> m;

int x=10;

map<int,int>::iterator ite;

ite=m.lower_bound(x);//返回比第一个大于或等于x的值的位置 ,当m为空时,返回m.begin()

ite=m.upper_bound(x);//返回比最后一个大于或等于x的值的位置

Leave a Reply

Your email address will not be published. Required fields are marked *