ใน 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