当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。