Home Backend Development C++ Can I Use Pairs as Keys in C Unordered Maps, and How?

Can I Use Pairs as Keys in C Unordered Maps, and How?

Dec 16, 2024 pm 02:11 PM

Can I Use Pairs as Keys in C   Unordered Maps, and How?

Can I use Pairs as Keys in Unordered Maps?

In C , you can encounter a compilation error when using an unordered_map with pair as its key. The issue arises from utilizing a custom type as the key, without providing an appropriate hash function for it.

Understanding the Error

The error you encounter is because unordered_map relies on a hashing mechanism to efficiently locate key-value pairs. However, it lacks a default hash function for complex types like pairs. The error message indicates that the compiler cannot implicitly instantiate an undefined hash function for your pair key type.

Solution: Providing a Hash Function

To resolve this issue, you need to define a hash function that calculates a unique hash value for your Vote pair type. This function should accept a pair as an argument and output a hash value.

Here's an example hash function implementation:

struct pair_hash {
    template<class T1, class T2>
    std::size_t operator()(const std::pair<T1, T2> &p) const {
        auto h1 = std::hash<T1>()(p.first);
        auto h2 = std::hash<T2>()(p.second);
        return h1 ^ h2;
    }
};
Copy after login

With this hash function, you can modify your code to create an unordered_map that uses pairs as keys:

using Vote = std::pair<std::string, std::string>;
using Unordered_map = std::unordered_map<Vote, int, pair_hash>;
Copy after login

Now, you can use this unordered_map as intended, avoiding the compilation error.

The above is the detailed content of Can I Use Pairs as Keys in C Unordered Maps, and How?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

What are the types of values ​​returned by c language functions? What determines the return value?

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

What are the definitions and calling rules of c language functions and what are the

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

Where is the return value of the c language function stored in memory?

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

How does the C Standard Template Library (STL) work?

See all articles