C++ complex type as the key value of unordered_map

unordered_map & unordered_set

We know that there are two data structures of unordered_map and unordered_set in C++, and its internal implementation is a hash table, which requires that the key value type must be hashable, such as common data types int, string, etc. But in many applications, we may need to use more complex types as key values, such as vector<int>, pair<int,int>, or even custom classes. At this time, we need to manually write two classes to create the data Let's take a look at the parameters of the structure object.

Now I want pair<int,int> to be the key value of unorderd_map. I need to write two classes. Each class contains a function. The function in the first class is called a hash function. Set a pair<int, The object of int> is hashed, and its hash value is returned; the second is called the equality function, which is used to determine whether two pair<int,int> objects are equal, which is intended to resolve conflicts.

ps: Class and structure are actually similar and can be used instead of each other

struct Hashfunc {
    
    
    size_t operator() (const pair<int,int>& key) const{
    
    
        return hash<int>()(key.first) ^ hash<int>()(key.second);
    }
};
struct Equalfunc {
    
    
    bool operator() (const pair<int,int>& a, const pair<int,int>& b) const{
    
    
        return a.first == b.first && a.second == b.second;
    }
};

The specific implementation of these two classes is completely up to you, but you need to pay attention, because these two classes are passed to unordered_map as parameters, so certain rules need to be met when defining them. For example, the return value of Hashfunc is size_t. The judgment function in Equalfunc must add a const field.

After writing the above two classes, pass them to the data structure object to be created, and then you can use it.

unordered_map<pair<int,int>, int, Hashfunc, Equalfunc> mp;

map & set

The above is about the application of unordered_map and unordered_set. What about map and set? We know that the internal implementation of map and set is a tree. Since the binary search tree is used, there must be a way to compare the size of the two elements. The pair<int,int> and vector<int> mentioned above, it seems that map is automatically provided Give it a method of comparison, so it can be used directly as a key value without having to define the class yourself.

When do you need to define your own class? The answer is when using a custom class as the key value. Similar to the above, we now define a class, the function in the class we also give it a name, let's call it the less than function, this function is used to determine the size of the two element values.
as follows:

class T {
    
    
	public:
	int a,b;
	T(int c, int d) {
    
    
		a = c;
		b = d;
	}
};
struct Tless {
    
    
	bool operator() (const T& a, const T& b) {
    
    
		return a.a < b.a;
	}
};
map<T, int, tless> mp2;

Guess you like

Origin blog.csdn.net/weixin_43867940/article/details/105775739