C++ set::lower_bound/upper_bound

2 篇文章 0 订阅
2 篇文章 0 订阅

 C++ set::lower_bound/upper_bound

 

最近对C++ set::lower_bound/upper_bound ,进行了一些测试,就是如果set当中为空时,使用lower_bound/upper_bound 都回返回begin 地址,也就是第一个插入的位置。对应的begin和end 是同一个位置

// set::lower_bound/upper_bound
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<int>::iterator itlow,itup;

  itlow=myset.lower_bound (30);                //       ^
  itup=myset.upper_bound (60);                 //                   ^

	if(itlow == myset.begin()) printf("ok!\n");
	if(itup == myset.begin()) printf("ok!\n");
	if(myset.end() == myset.begin()) printf("ok!\n");

  return 0;
}

至于使用,就是看正常的官方文档就行

// set::lower_bound/upper_bound
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<int>::iterator itlow,itup;

  for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90

  itlow=myset.lower_bound (30);                //       ^
  itup=myset.upper_bound (60);                 //                   ^

  myset.erase(itlow,itup);                     // 10 20 70 80 90

  std::cout << "myset contains:";
  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

 Notice that lower_bound(30) returns an iterator to 30, whereas upper_bound(60) returns an iterator to 70.

myset contains: 10 20 70 80 90
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值