কম্পিউটার টিউটোরিয়াল

C++-এ ইউজার ডিফাইনড ক্লাসের unordered_map তৈরি করা


এই টিউটোরিয়ালে আমরা C++-এ ইউজার ডিফাইনড ক্লাসের unordered_map কীভাবে তৈরি করতে হয় সেটা নিয়ে আলোচনা করব।

ইউজার ডিফাইনড ক্লাস থেকে unordered_map তৈরি করতে, আমরা হ্যাশ ফাংশনকে ক্লাস মেথড হিসেবে থার্ড আর্গুমেন্ট হিসেবে পাস করব।

উদাহরণ

#include <bits/stdc++.h> using namespace std; //objects of class to be used as key values struct Person {    string first, last;    Person(string f, string l){       first = f;       last = l;    }    bool operator==(const Person& p) const{       return first == p.first && last == p.last;    } }; class MyHashFunction {    public:    //using sum of length as hash function    size_t operator()(const Person& p) const{       return p.first.length() + p.last.length();    } }; int main(){    unordered_map<Person, int, MyHashFunction> um;    Person p1("kartik", "kapoor");    Person p2("Ram", "Singh");    Person p3("Laxman", "Prasad");    um[p1] = 100;    um[p2] = 200;    um[p3] = 100;    for (auto e : um) {       cout << "[" << e.first.first << ", "<< e.first.last<< "] = > " << e.second << '\n';    }    return 0; }

আউটপুট

[Laxman, Prasad] = > 100 [kartik, kapoor] = > 100 [Ram, Singh] = > 200

  1. MongoDB v3-এ ইউজার তৈরি করার পদ্ধতি

  2. OpenCV-এ C++ ব্যবহার করে বাইনারি ইমেজ তৈরি করা

  3. পাইথনে নেমস্পেস তৈরি করার উপায়

  4. পাইথনে ইউজার-ডিফাইনড এক্সসেপশন ইমপ্লিমেন্ট করা