Horje
user defined key for map in c++ Code Example
user defined key for map in c++
// how to create user defined key in map in c++
template<typename T1, typename T2>
struct ele
{
    T1 first;
    T2 second;
 
    // constructor
    
    ele(T1 a, T2 b): first(a), second(b) {}  // assigning first = a and second = b;
 
    // only thing remaining is to overload the '<' operator so that key comparator knows how
    // to compare keys while storing values in map
    
    // it will return true if my first value of first key is smaller than fisrt value of
    // other key(here other key is 'e') or both have same first value but second value of 
    // first key is smaller than second value of other key, else it will return false;
    bool operator<(const ele &e) const {
        return first < e.first || (first == e.first && second < e.second);
    }
};

// while declaring map
// how to declare a map with 4 different types

 map<ele<long long , long long>, ll>m; // map of key type long long , long long
 map<ele<string , string>, ll>m; // map of key type string ,string
 map<ele<char , char>, ll>m; // map of key type char , char
 map<ele<int , char>, ll>m; // map of key type int , char


// while declaring object of ele type the make sure to provdie the typename . for ex- 
ele<long long, long long> e1;
ele<int, char> e2;
ele<string, string> e3;




Cpp

Related
cpp code for euclids GCD Code Example cpp code for euclids GCD Code Example
C++ Area of a Rectangle Code Example C++ Area of a Rectangle Code Example
C++ Arithmetic Operators Code Example C++ Arithmetic Operators Code Example
retourner pointeur de type qstringlist qt Code Example retourner pointeur de type qstringlist qt Code Example
how to set a string equal to another string cpp Code Example how to set a string equal to another string cpp Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8