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;

引數

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
廣告
© . All rights reserved.