C++ Map 库 - upper_bound() 函数



描述

C++函数std::map::upper_bound()返回一个迭代器,指向第一个大于键k的元素。k.

声明

以下是来自``头文件的std::map::upper_bound()函数声明。

C++98

iterator upper_bound (const key_type& k);
const_iterator upper_bound (const key_type& k) const;

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

参数

k − 要搜索的键。

返回值

如果对象是常限定的,则方法返回一个常量迭代器,否则返回非常量迭代器。

异常

此成员函数不抛出异常。

时间复杂度

对数,即 O(log n)

示例

以下示例演示了std::map::upper_bound()函数的使用。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   auto it = m.upper_bound('b');

   cout << "Upper bound is " << it->first << 
      " = " << it->second << endl;

   return 0;
}

让我们编译并运行上述程序,这将产生以下结果:

Upper bound is c = 3
map.htm
广告