Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph does not support std::pair as vertex type #56

Closed
lxylxy123456 opened this issue May 15, 2021 · 1 comment
Closed

Graph does not support std::pair as vertex type #56

lxylxy123456 opened this issue May 15, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@lxylxy123456
Copy link
Owner

To reproduce, try to compile:

#include "Graph.hpp"
int main() {
	algorithms::GraphAdjList<std::pair<int, int>> a(1);
	return 0;
}
@lxylxy123456 lxylxy123456 added the bug Something isn't working label May 15, 2021
@lxylxy123456
Copy link
Owner Author

This is expected, as C++ does not provide a hash function for std::pair by default.
A workaround (some code adapted from https://en.cppreference.com/w/cpp/utility/hash):

#include "Graph.hpp"

namespace std
{
    template<> struct hash<std::pair<int, int>>
    {
        std::size_t operator()(std::pair<int, int> const& s) const noexcept
        {
            std::size_t h1 = std::hash<int>{}(s.first);
            std::size_t h2 = std::hash<int>{}(s.second);
            return h1 ^ (h2 << 1); // or use boost::hash_combine
        }
    };
}

int main() {
	algorithms::GraphAdjList<std::pair<int, int>> a(1);
	// std::hash<std::pair<int, int>> a;
	return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant