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


C++ unordered_map key_eq()用法及代碼示例


unordered_map::key_eq()是C++ STL中的內置函數,該函數根據比較結果返回布爾值。它取決於unordered_map容器使用的鍵等效項比較謂詞。 key 等效項比較是一個謂詞,該謂詞接受兩個參數並返回一個布爾值,該布爾值指示是否將它們視為等效。如果它們相等則返回true,否則返回false。它在構造時由容器采用,類似於比較中使用的(==)運算符。

用法

unordered_map_name.key_eq()(args1, args2)

參數:該函數接受兩個強製性參數args1和args2,在這兩個參數之間進行比較。 data_type與unordered_map相同。


返回值:該函數返回一個布爾值。

以下示例程序旨在說明unordered_map::key_eq()函數。

範例1:

// CPP program to illustrate the 
// unordered_map::key_eq() function 
  
#include <bits/stdc++.h> 
  
using namespace std; 
  
int main() 
{ 
    // Declaring unordered_map 
    unordered_map<string, string> sample; 
  
    // check details 
    bool answer 
        = sample.key_eq()("GEEKS", "geeks"); 
  
    // checks if both are same 
    if (answer) 
        cout << "GEEKS and geeks are treated"
             << " similarly in the container\n"; 
    else
        cout << "GEEKS and geeks are treated"
             << " dissimilarly in the container\n"; 
  
    return 0; 
}
輸出:
GEEKS and geeks are treated dissimilarly in the container

範例2:

// CPP program to illustrate the 
// unordered_map::key_eq() function 
  
#include <bits/stdc++.h> 
  
using namespace std; 
  
int main() 
{ 
  
    unordered_map<int, int> sample; 
  
    bool answer = sample.key_eq()(100, 200); 
  
    // check 
    if (answer) 
        cout << "100 and 200 are treated "
             << "similarly in the container\n"; 
    else
        cout << "100 and 200 are treated"
             << " dissimilarly in the container\n"; 
  
    answer = sample.key_eq()(100, 100); 
    if (answer) 
        cout << "100 and 100 are treated "
             << "similarly in the container\n"; 
    else
        cout << "100 and 100 are treated "
             << "dissimilarly in the container\n"; 
  
    return 0; 
}
輸出:
100 and 200 are treated dissimilarly in the container
100 and 100 are treated similarly in the container


相關用法


注:本文由純淨天空篩選整理自ankit15697大神的英文原創作品 unordered_map key_eq() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。