📜  C++ STL中的multimap upper_bound()函数

📅  最后修改于: 2021-05-30 17:06:55             🧑  作者: Mango

multimap :: upper_bound(k)是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向刚好大于k的下一个元素。如果参数中传递的键超过了容器中的最大键,则迭代器返回的键指向key + 1,element = 0。

句法:

multimap_name.upper_bound(key)

参数:该函数接受单个强制性参数键,该键指定返回其lower_bound的元素。

返回值:该函数返回一个迭代器,该迭代器指向刚好大于k的紧邻的下一个元素。如果参数中传递的键超过了容器中的最大键,则迭代器返回的键指向key + 1,element = 0。

// C++ function for illustration
// multimap::upper_bound() function
#include 
using namespace std;
  
int main()
{
  
    // initialize container
    multimap mp;
  
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 2, 60 });
    mp.insert({ 2, 20 });
    mp.insert({ 1, 50 });
    mp.insert({ 4, 50 });
  
    // when 2 is present
    auto it = mp.upper_bound(2);
    cout << "The upper bound of key 2 is ";
    cout << (*it).first << " " << (*it).second << endl;
  
    // when 3 is not present
    it = mp.upper_bound(3);
    cout << "The upper bound of key 3 is ";
    cout << (*it).first << " " << (*it).second << endl;
  
    // when 5 is exceeds the maximum key
    it = mp.upper_bound(5);
    cout << "The upper bound of key 5 is ";
    cout << (*it).first << " " << (*it).second;
    return 0;
}
输出:
The upper bound of key 2 is 4 50
The upper bound of key 3 is 4 50
The upper bound of key 5 is 6 0
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”