multiset upper_bound() 与 lower_bound()

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 container
    s.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 present
    auto 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 0
    it = 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 5
    it = s.lower_bound(5);
    cout << "The lower bound of key 5 is ";
    cout << (*it) << endl;
  
    // when 8 exceeds the max element in multiset
    it = 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
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

scott198512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值