Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 映射v.s.无序_映射中密钥的容许类型_C++_C++11_Stl - Fatal编程技术网

C++ 映射v.s.无序_映射中密钥的容许类型

C++ 映射v.s.无序_映射中密钥的容许类型,c++,c++11,stl,C++,C++11,Stl,我了解,从键O(1)检索值时,unordered_map比map在性能方面的效率更高 但是,我发现,unordered_-map不支持像map那样多的类型 例如,map可以,但unordered\u map不行 我想知道这有什么潜在的困难吗?如果考虑到性能,我可以使用什么数据结构来获得具有O(1)性能的密钥类型对哈希表 它不能开箱即用的原因是类型对没有定义散列函数,无序映射本质上是散列映射,因此它的键必须是可散列的 无序的地图从根本上说没有什么问题。问题是您需要提供一个哈希函数和一个比较函数,以

我了解,从键O(1)检索值时,
unordered_map
map
在性能方面的效率更高

但是,我发现,
unordered_-map
不支持像
map
那样多的类型

例如,
map
可以,但
unordered\u map
不行


我想知道这有什么潜在的困难吗?如果考虑到性能,我可以使用什么数据结构来获得具有O(1)性能的密钥类型对哈希表

它不能开箱即用的原因是类型
没有定义散列函数,
无序映射
本质上是散列映射,因此它的键必须是可散列的

无序的地图从根本上说没有什么问题。问题是您需要提供一个哈希函数和一个比较函数,以实现相等


有关
作为键的具体情况以及一般情况的更多详细信息,请参阅。

它不能立即工作的原因是,类型
没有定义哈希函数,
无序映射
本质上是一个哈希映射,因此其键必须是可哈希的

无序的地图从根本上说没有什么问题。问题是您需要提供一个哈希函数和一个比较函数,以实现相等


有关
作为密钥的具体情况以及一般情况的更多详细信息,请参阅。

如果可以提供一种从
对生成哈希的方法,则可以使用它

例如:

#include <unordered_map>
#include <utility>
using namespace std;

struct IntPairHash
{
   size_t operator()(pair<int, int> const& p)
   {
      // If you have a 64 bit platform where sizeof(size_t) == 64
      // and sizeof(int) == 32, you can use:
      size_t h = p.second;
      h <<= 32;
      h += p.first;
      return h;

      // For other platforms, a more different approach to come up with
      // a hash value for p must be devised.
   }
};

void foo()
{
   unordered_map<pair<int, int>, int, IntPairHash> a;
}
#包括
#包括
使用名称空间std;
结构IntPairHash
{
size\u t运算符()(成对常量和p)
{
//如果您有一个64位平台,其中sizeof(size\u t)==64
//和sizeof(int)==32,可以使用:
尺寸h=p.s;

h如果您可以提供一种从
生成哈希的方法,那么您可以使用它

例如:

#include <unordered_map>
#include <utility>
using namespace std;

struct IntPairHash
{
   size_t operator()(pair<int, int> const& p)
   {
      // If you have a 64 bit platform where sizeof(size_t) == 64
      // and sizeof(int) == 32, you can use:
      size_t h = p.second;
      h <<= 32;
      h += p.first;
      return h;

      // For other platforms, a more different approach to come up with
      // a hash value for p must be devised.
   }
};

void foo()
{
   unordered_map<pair<int, int>, int, IntPairHash> a;
}
#包括
#包括
使用名称空间std;
结构IntPairHash
{
size\u t运算符()(成对常量和p)
{
//如果您有一个64位平台,其中sizeof(size\u t)==64
//和sizeof(int)==32,可以使用:
尺寸h=p.s;

h您可以定义自定义哈希函数:

#include <unordered_map>

typedef std::pair<int,int> int_pair;
class MyHashFunc {
    std::hash<int> int_hash_func;
    public:
        long operator()(const int_pair &k) const{

            return (int_hash_func(k.first) << 16) + (int_hash_func(k.second));
        }
};

int main() {
    std::unordered_map<int_pair, int, MyHashFunc> m;
    return 0;
}
#包括
typedef std::pair int_pair;
类MyHashFunc{
std::hash int\u hash\u func;
公众:
长运算符()(常量int_对&k)常量{

return(int_hash_func(k.first)您可以定义自定义哈希函数:

#include <unordered_map>

typedef std::pair<int,int> int_pair;
class MyHashFunc {
    std::hash<int> int_hash_func;
    public:
        long operator()(const int_pair &k) const{

            return (int_hash_func(k.first) << 16) + (int_hash_func(k.second));
        }
};

int main() {
    std::unordered_map<int_pair, int, MyHashFunc> m;
    return 0;
}
#包括
typedef std::pair int_pair;
类MyHashFunc{
std::hash int\u hash\u func;
公众:
长运算符()(常量int_对&k)常量{

return(int_hash_func(k.first))不清楚您是在问为什么它不能开箱即用,还是想知道如何使它起作用。到目前为止,答案都是针对后者的。@juanchopanza,观点很好。我更新了,试图解决第一个问题。@merlin2011好吧,我会解释为问“为什么
std::hash
没有专门用于
std:pair
T
是专门用于
std::hash
的类型:-)这是一个更好的复制品:不清楚您是在问为什么它不能开箱即用,还是想知道如何使它工作。目前的答案是后者。@juanchopanza,非常好的观点。我更新了以尝试解决第一个问题。@merlin2011好吧,我会解释为问“为什么
std::hash
没有专门用于
std:pair
其中
T
std::hash
专门用于的类型:-)这是一个更好的副本:
h@TonyD点做得很好。我更新了我的答案以添加该警告。
h@TonyD点做得很好。我更新了我的答案以添加该警告。