multiset::upper_bound()是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向刚好大于key的下一个元素。如果参数中传入的键超过了容器中的最大键,则返回的迭代器将指向一个元素,确切的说解引用后是一个index或者pos值,该数值对应容器中最后一个元素之后的位置。

* A built-in function in C++ STL that returns an iterator pointing to the immediate next element which is just greater than k.
** If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points an element which points to the position after the last element in the container

multiset_name.upper_bound(key)
#include <iostream>
#include <set>using namespace std;
int main()
{ multiset<int> s; // Function to insert elements // in the multiset container s.insert(1); s.insert(3); s.insert(3); s.insert(5); s.insert(4); s.insert(8);cout << "The multiset elements are:"; for (auto it = s.begin(); it != s.end(); it++) cout << *it << " "; // when 3 is present auto it = s.upper_bound(3); cout << "\nThe upper bound of key 3 is "; cout << (*it) << endl; // when 2 is not present // points to next greater after 2 it = s.upper_bound(2); cout << "The upper bound of key 2 is "; cout << (*it) << endl; // when 8 equal the max element in multiset it = s.upper_bound(8); cout << "The upper bound of key 8 is "; cout << (*it) << endl; // result is equivalent to exceeds the max element in multiset. // when 10 exceeds the max element in multiset it = s.upper_bound(10); cout << "The upper bound of key 10 is "; cout << (*it) << endl; // get a index  which is just greater than max element in multiset// the max element in multiset is 8, which index is 5, so we get a new position is 6.return 0;
}

Output:

The multiset elements are:1 3 3 4 5 8
The upper bound of key 3 is 4
The upper bound of key 2 is 3
The upper bound of key 8 is 6
The upper bound of key 10 is 6

multiset::lower_bound()是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向刚好等于key的一个元素。如果参数中传递的键不在容器中,则返回的迭代器将指向刚好大于key的那个元素,如果传入的key超过了容器中的最大键,则返回的迭代器将指向一个元素确切的说解引用后是一个index或者pos值,该数值对应容器中最前面一个元素之后的位置。

* A built-in function in C++ STL which returns an iterator pointing to the first element in the container which is equivalent to k passed in the parameter.
** In case k is not present in the set container, the function returns an iterator pointing to the immediate next element which is just greater than k.
*** If the key passed in the parameter exceeds the maximum value in the container, then the iterator returned point the number of elements in the container

multiset_name.lower_bound(key)
#include <iostream>
#include <set>using namespace std;int main()
{multiset<int> s;// Function to insert elements// in the multiset containers.insert(2);s.insert(3);s.insert(3);s.insert(2);s.insert(6);cout << "The multiset elements are: ";for (auto it = s.begin(); it != s.end(); it++)cout << *it << " "; // order by key automaticly, from little to bigger// when 3 is presentauto it = s.lower_bound(3);cout << "\nThe lower bound of key 3 is ";cout << (*it) << endl;// when 0 is not present// points to next element just greater after 0it = s.lower_bound(0);cout << "The lower bound of key 0 is ";cout << (*it) << endl;// when 5 is not present// points to next element just greater after 5it = s.lower_bound(5);cout << "The lower bound of key 5 is ";cout << (*it) << endl;// when 8 exceeds the max element in multisetit = s.lower_bound(8);cout << "The lower bound of key 8 is ";cout << (*it) << endl; // get a index or pos which is just greater than the max element in multiset// the max element in multisetis 6, which pos is 4, so we get a new position is 5.return 0;
}

Output:

The multiset elements are: 2 2 3 3 6
The lower bound of key 3 is 3
The lower bound of key 0 is 2
The lower bound of key 5 is 6
The lower bound of key 8 is 5

multiset upper_bound() 与 lower_bound()相关推荐

  1. upper_bound()与lower_bound函数的使用

    1679: 查找2 Time Limit: 1 Sec Memory Limit: 128 MB [Submit][Status][Web Board] Description 给你一个长度是n的序列 ...

  2. upper_bound()与lower_bound()

    2018-2-5 upper_bound 和lower_bound是STL中二分查找的函数,所以效率会比较高. 首先,最形象的一句话: upper_bound(i) 返回的是键值为i的元素可以插入的最 ...

  3. 浅谈c++中upper_bound与lower_bound的用法

    神犇勿喷 在我还是个蒟蒻的时候(现在还是哈),我在网上翻了几篇有关upper_bound和lower_bound的文章,结果发现我全都看不懂非常复杂,为了避免再有人重蹈覆辙,我就发了这篇粗制滥造简单易 ...

  4. C++primer学习:关于upper_bound和lower_bound的探究.

    今天花了将近一个小时仔细研究了这两个范型算法以及关联容器定义的同名函数.下面就来讲一讲它们的用法和一些细节. [1]首先是范型算法upper_bound与lower_bound;它们要求必须提供至少前 ...

  5. upper_bound和lower_bound用法(史上最全)

    目录 基础用法 用greater<type>()重载 进阶用法(自定义匿名函数) upper_bound进阶 lower_bound进阶 所有代码 两者都是定义在头文件<algori ...

  6. C++ upper_bound()和lower_bound()(二分查找中使用)的定义,使用方法和区别

    C++ upper_bound()和lower_bound()是涉及二分查找问题一个很好用的工具,熟练使用就不用为二分查找的边界发愁了(不用重复造轮子了) 1. 调用方式 upper_bound有两种 ...

  7. (转)upper_bound()与lower_bound()使用方法

    1 2 3 4 5 6 7 8 9 10 11 12 13 #include<iostream> #include<algorithm>//必须包含的头文件 #include& ...

  8. 二分查找、upper_bound、lower_bound

    整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch;import java.util.Arrays;/*** @author jerry* @cre ...

  9. STL 二分查找 upper_bound和lower_bound用法

    STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search . 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数. ...

最新文章

  1. Linux 初始化脚本 (centos6 centos7 通用)
  2. Sitemesh 3 的使用及配置
  3. 学习笔记11-C语言-指针
  4. java有没有number数据类型_Java基本数据类型之Number
  5. Red Hat Enterprise Linux 7 启动过程
  6. ActivityMQ应用详解
  7. Kubernetes 系统强化 Pod安全策略 PSP
  8. MAC M1 安装 matlab2020a
  9. nanohttpd:实现跨域(CORS)请求
  10. 计算机设备没有音频,电脑没有音频设备怎么办
  11. 软件工程师欲发动DDoS攻击白宫网站 抗议特朗普就任总统
  12. 为什么我们会有假期一结束,快乐就终止的感觉?
  13. 服务器拒绝mac访问共享文件,Mac怎样访问局域网共享文件
  14. 信息流推广与普通搜索推广的区别与优势是什么?
  15. c语言与程序设计曹计昌 答案,c语言与程序设计答案曹计昌
  16. 小程序以及h5引入阿里图标不显示最简单解决方案
  17. UML软件建模技术-基于IBM RSA工具的基础实训
  18. 《oracle数据库》基本查询语句
  19. 【知识蒸馏】什么是知识蒸馏、方法解读
  20. android穿山甲广告位id,Android自渲染Draw广告

热门文章

  1. EasyExcel对列同类项进行单元格合并
  2. 7.1 项目成本管理
  3. 教您一招解决Word不能复制粘贴问题
  4. 微星B360傲腾内存设置方法
  5. 【生活】年化收益率、七日年化收益率这些事
  6. 艾洛积分系统(Elo Rating System)
  7. 金蝶服务器响应异常,金蝶提示连接金蝶云服务器异常
  8. 《传智播客.Net培训.net视频教程》(.net视频asp.net培训传智播客asp.net视频教程开放课程c#视频移动开发winform SQL ADO.Net HTML JavaScript
  9. systemverilog中automatic与static
  10. IC/FPGA一文练完