标签: unordered-set

如何在无序容器中为用户定义的类型专门化std :: hash <Key> :: operator()?

为了支持用户定义的键类型std::unordered_set<Key>std::unordered_map<Key, Value> 一个具有提供operator==(Key, Key)和散列函子:

struct X { int id; /* ... */ };
bool operator==(X a, X b) { return a.id == b.id; }

struct MyHash {
  size_t operator()(const X& x) const { return std::hash<int>()(x.id); }
};

std::unordered_set<X, MyHash> s;
Run Code Online (Sandbox Code Playgroud)

std::unordered_set<X> 使用类型的默认哈希来编写会更方便X,就像编译器和库中的类型一样.咨询后

似乎可以专门化std::hash<X>::operator():

namespace std { // argh!
  template <>
  inline size_t 
  hash<X>::operator()(const X& x) const { return …
Run Code Online (Sandbox Code Playgroud)

c++ hash unordered-map unordered-set c++11

95
推荐指数
2
解决办法
5万
查看次数

如何在C++中创建一组无序的整数对?

以下程序不会编译一组无序的整数对,但它会对整数进行编译.Can unordered_set和它的成员函数可以用在用户定义的类型上,我该如何定义它?

#include <unordered_set>
...

class A{
...
private: 
    std::unordered_set< std::pair<int, int> > u_edge_;
};
Run Code Online (Sandbox Code Playgroud)

编译错误:

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

c++ unordered-set std-pair

42
推荐指数
6
解决办法
3万
查看次数

如何在C++中迭代无序集?

假设我有一个无序集

unordered_set<int> my_set;
myset.insert(1);
myset.insert(2);
myset.insert(3);
Run Code Online (Sandbox Code Playgroud)

我如何迭代它?我不需要以任何顺序迭代 - 只要我到达每个元素一次.我试过了

for (int i = 0; i < my_set.size(); i++)
     cout << my_set[i];
Run Code Online (Sandbox Code Playgroud)

无济于事.

c++ iteration unordered-set c++11

32
推荐指数
2
解决办法
5万
查看次数

unordered_map/unordered_set中元组的通用哈希

为什么不std::unordered_map<tuple<int, int>, string>开箱即用?必须为tuple<int, int>例如定义散列函数是繁琐的

template<> struct do_hash<tuple<int, int>>                               
{   size_t operator()(std::tuple<int, int> const& tt) const {...}  }; 
Run Code Online (Sandbox Code Playgroud)

构建一个以元组为键的无序映射(Matthieu M.)展示了如何自动执行此操作boost::tuple.有没有为c ++ 0x元组执行此操作而不使用可变参数模板?

当然这应该在标准:(

c++ unordered-map tuples unordered-set c++11

27
推荐指数
4
解决办法
2万
查看次数

标准容器没有std :: hash的特化吗?

只是发现自己有点吃惊暂时无法简单地用一个

std::unordered_set<std::array<int, 16> > test;
Run Code Online (Sandbox Code Playgroud)

因为s 似乎没有std::hash专业化std::array.这是为什么?或者我根本找不到它?如果确实没有,可以简化以下实施尝试吗?

namespace std
{
    template<typename T, size_t N>
    struct hash<array<T, N> >
    {
        typedef array<T, N> argument_type;
        typedef size_t result_type;

        result_type operator()(const argument_type& a) const
        {
            hash<T> hasher;
            result_type h = 0;
            for (result_type i = 0; i < N; ++i)
            {
                h = h * 31 + hasher(a[i]);
            }
            return h;
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

我真的觉得这应该成为标准库的一部分.

c++ arrays hash-function unordered-set c++11

26
推荐指数
2
解决办法
4191
查看次数

使用std :: unordered_set的std :: unique_ptr

假设我有一组unique_ptr:

std::unordered_set <std::unique_ptr <MyClass>> my_set;
Run Code Online (Sandbox Code Playgroud)

我不确定检查集合中是否存在给定指针的安全方法是什么.这样做的正常方法可能是调用my_set.find (),但我作为参数传递什么?

我从外面得到的只是一个原始指针.所以我必须从指针创建另一个unique_ptr,将它传递给find()然后release()指针,否则对象将被破坏(两次).当然,这个过程可以在一个函数中完成,因此调用者可以传递原始指针并进行转换.

这种方法安全吗?有没有更好的方法来使用一组unique_ptr?

c++ stl unordered-set unique-ptr c++11

26
推荐指数
4
解决办法
6048
查看次数

std::unordered_set 如何存在病理输入?

我正在解决在给定数组中找到不同整数的数量的基本问题。

我的想法是声明一个std::unordered_set,将所有给定的整数插入到集合中,然后输出集合的大小。这是我实现此策略的代码:

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <unordered_set>

using namespace std;

int main()
{
    int N;
    cin >> N;
    
    int input;
    unordered_set <int> S;
    for(int i = 0; i < N; ++i){
        cin >> input;
        S.insert(input);
    }
    
    cout << S.size() << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这种策略几乎适用于所有输入。在其他输入情况下,它超时。

我很好奇我的程序为什么会超时,所以我cout << i << endl;在 for 循环中添加了一行。我发现当我进入输入案例时,53000循环的第一次左右迭代几乎会立即通过,但之后100每秒只会发生几次迭代。

我已经阅读了O(N)如果发生大量冲突,散列集如何以插入结束,所以我认为输入在std::unordered_set.

然而,这是不可能的。std::unordered_set用于整数的哈希函数将它们映射到自身(至少在我的计算机上),因此不同整数之间不会发生冲突。我使用写在这个链接上的代码访问了哈希函数。

我的问题是,输入本身是否有可能 …

c++ hashset unordered-set data-structures

25
推荐指数
2
解决办法
942
查看次数

我如何使用unordered_set?

我试图定义一个像这样的unordered_set:

unordered_set<Point> m_Points;
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我收到以下错误:

C++标准不为此类型提供哈希.

课程Point:

class Point{
    private:
        int x, y;
    public:
        Point(int a_x, int a_y)
            : x(a_x), y(a_y)
        {}
        ~Point(){}

        int getX()const { return x; }
        int getY()const { return y; }

        bool operator == (const Point& rhs) const{
            return x == rhs.x && y == rhs.y;
        }

        bool operator != (const Point& rhs) const{
            return !(*this == rhs);
        }
};
Run Code Online (Sandbox Code Playgroud)
  • 如何/在哪里为Point定义哈希函数?
  • 什么是2D点的好哈希函数?

c++ stl set unordered-set

23
推荐指数
1
解决办法
4万
查看次数

将向量的元素添加到无序集

有没有一种简单的方法可以将a的所有元素添加vectorunordered_set?它们属于同一类型.现在,我正在使用for循环,并想知道是否有更好的方法来做到这一点

c++ vector unordered-set

22
推荐指数
2
解决办法
2万
查看次数

使用自定义哈希函数插入unordered_set

我有以下代码来制作unordered_set<Interval>.编译好了.

struct Interval {
  unsigned int begin;
  unsigned int end;
  bool updated;   //true if concat.  initially false
  int patternIndex;  //pattern index. valid for single pattern
  int proteinIndex;   //protein index.  for retrieving the pattern
};

struct Hash {
  size_t operator()(const Interval &interval);
};

size_t Hash::operator()(const Interval &interval){
  string temp = to_string(interval.begin) + to_string(interval.end) + to_string(interval.proteinIndex);
  return hash<string>()(temp);
}

unordered_set<Interval, string, Hash> test;
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用此代码插入时,我无法编译:

for(list<Interval>::iterator i = concat.begin(); i != concat.end(); ++i){
  test.insert((*i));
}
Run Code Online (Sandbox Code Playgroud)

而且,我无法确定错误消息的问题,例如:

note: candidate is:
note: size_t …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-set c++11

22
推荐指数
1
解决办法
3万
查看次数