lower_bound 用法

template <class ForwardIterator, class T>
  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
                                const T& value );

template <class ForwardIterator, class T, class Compare>
  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
                                const T& value, Compare comp );
<algorithm>

Return iterator to lower bound

Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value. The comparison is done using either operator< for the first version, or comp for the second.

For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).

Unlike upper_bound, this function returns an iterator to the element also if it compares equivalent to value and not only if it compares greater.

The behavior of this function template is equivalent to:

template <class ForwardIterator, class T>
  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
  ForwardIterator it;
  iterator_traits<ForwardIterator>::distance_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;
}

Parameters

first, last
Forward iterators to the initial and final positions of the sequence to use. The range used is  [first,last), which contains all the elements between  first and  last, including the element pointed by  first but not the element pointed by  last.
value
Element value to be checked for its lower bound.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns  true if the first argument goes before the second argument in the specific  strict weak ordering it defines, and false otherwise.

Return value

Iterator to the lower bound position for  value in the range.
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class student{
	public:
		int key;
		int value;
/*	bool operator()(student a,student b){
		return a.key<b.key;
	}*/
};
bool cmp(student a,student b){
	return a.key<b.key;
}
int main(){
	vector<student> vec(6);
	int i;
	vector<student>::iterator low;
	vec[0].key=15;
	vec[0].value=1;
	vec[1].key=2;
	vec[1].value=2;
	vec[2].key=2;
	vec[2].value=2;;
	vec[3].key=3;
	vec[3].value=3;
	vec[4].key=3;
	vec[4].value=4;
	vec[5].key=7;
	vec[5].value=5;
	sort(vec.begin(),vec.end(),cmp);
	for(i=0;i<6;i++)
		cout<<vec[i].key<<endl;
	low=lower_bound(vec.begin(),vec.end(),vec[3],cmp);
	cout<<int(low-vec.begin())<<endl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值