📜  C++ STL中的unordered_map key_eq()函数

📅  最后修改于: 2021-05-30 09:22:10             🧑  作者: Mango

unordered_map :: key_eq()是C++ STL中的内置函数,根据比较结果返回布尔值。它取决于unordered_map容器使用的键等效项比较谓词。密钥等效项比较是一个谓词,该谓词接受两个参数并返回一个布尔值,该布尔值指示它们是否被视为等效。如果它们相等则返回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 
  
using namespace std;
  
int main()
{
    // Declaring unordered_map
    unordered_map 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 
  
using namespace std;
  
int main()
{
  
    unordered_map 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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”