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



描述

C++ 函式 std::map::key_comp() 返回一個用於比較鍵的函式物件,它是該容器建構函式引數的副本comp.

宣告

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

C++98

key_compare key_comp() const;

引數

返回值

返回一個鍵比較函式物件。

異常

此成員函式不丟擲異常。

時間複雜度

常數,即 O(1)

示例

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

#include <iostream>
#include <map>

using namespace std;

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

   auto comp = m.key_comp();
   char last = m.rbegin()->first;
   auto it = m.begin();

   cout << "Map contains following elements" << endl;

   do
      cout << it->first << " = " << it->second << endl;
   while (comp((*it++).first, last));

   return 0;
}

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

Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
map.htm
廣告

© . All rights reserved.