如何在C ++中制作无序的整数对对? [英] How can I make an unordered set of pairs of integers in C++?

查看:91
本文介绍了如何在C ++中制作无序的整数对对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序不会编译整数对的无序集合,但是会编译整数。 unordered_set 及其成员函数可以用于用户定义的类型,如何定义它?

The following program does not compile an unordered set of pairs of integers, but it does for integers. Can unordered_set and its member functions be used on user-defined types, and how can I define it?

#include <unordered_set>
...

class A{
...
private: 
    std::unordered_set< std::pair<int, int> > u_edge_;
};

编译器错误:


错误:没有匹配函数调用'std :: unordered_set> :: unordered_set()'

error: no matching function for call to 'std::unordered_set >::unordered_set()'


推荐答案

您的代码可以在VS2010 SP1(VC10)上编译,但是无法在GCC g ++ 4.7.2上编译。

Your code compiles on VS2010 SP1 (VC10), but it fails to compile with GCC g++ 4.7.2.

但是,您可能想要考虑从 Boost进行的 boost :: hash .Functional 散列 std :: pair (加上此代码,您的代码也可以使用g ++进行编译)。

However, you may want to consider boost::hash from Boost.Functional to hash a std::pair (with this addition, your code compiles also with g++).

#include <unordered_set>
#include <boost/functional/hash.hpp>

class A
{
private: 
    std::unordered_set< 
        std::pair<int, int>, 
        boost::hash< std::pair<int, int> > 
    > u_edge_;
};

这篇关于如何在C ++中制作无序的整数对对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆