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的值的位置

发表评论

电子邮件地址不会被公开。 必填项已用*标注