Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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;
Comment

PREVIOUS NEXT
Code Example
Cpp :: cout char32_t c++ 
Cpp :: how to hide ui elements unity 
Cpp :: reading in lines from a file to a vector c++ 
Cpp :: winmain example 
Cpp :: convert vector into array c++ 
Cpp :: cpp float to int 
Cpp :: how to clear screen in C++ console 
Cpp :: c++ find largest number in array 
Cpp :: format c++ discord 
Cpp :: minimum and maximum value of a vector in C++ 
Cpp :: string count occurrences c++ 
Cpp :: 2d array using vector 
Cpp :: how to get input in cpp 
Cpp :: in c++ ++ how to write if without if 
Cpp :: c++ shared pointer 
Cpp :: queue implementation using linked list in cpp 
Cpp :: convert long int to binary string c++ 
Cpp :: calculator c++ 
Cpp :: c++ open all files in directory 
Cpp :: how to convert int to std::string 
Cpp :: check if file is empty c++ 
Cpp :: rand c++ 
Cpp :: error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ 
Cpp :: convert refference to pointer c++ 
Cpp :: vector search by element 
Cpp :: all possible permutations of characters in c++ 
Cpp :: 1d array 
Cpp :: c++ remove numbers from vector if larger than n 
Cpp :: c++ loop vector 
Cpp :: how to code string to int converter c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =