检查键是否出现在 C++ map 或 unordered_map 中
在 C++ 中,地图和无序地图是哈希表。它们使用一些键及其各自的键值。下面我们将了解如何检查给定的键是否存在于哈希表中。代码如下 −
示例
#include<iostream>
#include<map>
using namespace std;
string isPresent(map<string, int> m, string key) {
if (m.find(key) == m.end())
return "Not Present";
return "Present";
}
int main() {
map<string, int> my_map;
my_map["first"] = 4;
my_map["second"] = 6;
my_map["third"] = 6;
string check1 = "fifth", check2 = "third";
cout << check1 << ": " << isPresent(my_map, check1) << endl;
cout << check2 << ": " << isPresent(my_map, check2);
}输出
fifth: Not Present third: Present
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP