當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Map crend()用法及代碼示例


描述

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

range由兩個迭代器定義,一個指向不小於key的第一個元素k另一個指向第一個大於鍵的元素k

聲明

以下是 std::map::equal_range() 函數形式 std::map 頭文件的聲明。

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

相關用法


注:本文由純淨天空篩選整理自 C++ Map Library - crend() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。