C++ 對映庫 - crend() 函式



描述

C++ 函式std::map::equal_range() 返回與特定鍵匹配的元素範圍。

該範圍由兩個迭代器定義,一個指向第一個不小於鍵的元素k另一個指向第一個大於鍵的元素。k.

宣告

以下是來自 std::map 標頭檔案的 std::map::equal_range() 函式的宣告。

C++98

pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
pair<iterator,iterator> equal_range (const key_type& k);

引數

k - 要搜尋的鍵。

返回值

如果物件被限定為常量,則該方法返回一對常量迭代器,否則返回一對非常量迭代器。

異常

此成員函式不丟擲異常。

時間複雜度

對數,即 O(log n)

示例

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

#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 ret = m.equal_range('b');

   cout << "Lower bound is " << ret.first->first <<
      " = " << ret.first->second << endl;

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

   return 0;
}

讓我們編譯並執行上述程式,這將產生以下結果:

Lower bound is b = 2
Upper bound is c = 3
map.htm
廣告