标签: unordered-set

std::unordered_set 中的 std::string_view 和 std::string

假设您有一个std::unordered_set<std::string>.

您有一个std::string_view要在容器中搜索的对象。问题是,您不想std::string从您的 中创建 a std::string_view,因为这种首先违背了使用的目的std::string_view

不过,好像std::string_view应该可以作为key使用;应该有某种方式来比较std::string_viewand std::string,因为它们基本上代表同一件事。但是没有,无论如何都没有在 STL 中。

这是一个僵局,我是否被迫编写自己的比较对象std::string_viewstd::string与我的对象一起使用std::unordered_set

编辑:这个问题特定于 string_view 对象。“重复”问题不相关。正如预期的那样,我收到了一个独特问题的独特答案。

c++ stl unordered-set string-view c++17

10
推荐指数
1
解决办法
3516
查看次数

tr1 :: hash for boost :: thread :: id?

我开始使用命名空间中的unordered_settr1来加速对普通(基于树的)STL的访问map.但是,我想在boost(boost::thread::id)中存储对线程ID的引用,并意识到这些标识符的API是如此不透明,以至于您无法清楚地获得它的哈希值.

令人惊讶的是,boost实现了tr1(包括hashunordered_set)的一部分,但它没有定义能够散列线程ID的哈希类.

查看boost::thread::id我发现的文档,发现线程ID可以输出到流,所以我的哈希解决方案是:

struct boost_thread_id_hash
{
    size_t operator()(boost::thread::id const& id) const
    {
        std::stringstream ostr;
        ostr << id;
        std::tr1::hash<std::string> h;
        return h(ostr.str());
    }
};
Run Code Online (Sandbox Code Playgroud)

也就是说,序列化它,将哈希应用于结果字符串.但是,这似乎比实际使用STL效率低map<boost::thread::id>.

所以,我的问题:您是否找到了更好的方法?在boost和tr1中是否明显不一致而不强迫hash<boost::thread::id>类的存在?

谢谢.

c++ hash boost boost-thread unordered-set

9
推荐指数
1
解决办法
2706
查看次数

Boost - unordered_set教程/ examples/ANYTHING?

我想unordered_set在一个项目中使用.

但是,它的文档要么不完整,要么只是技术参考,没有示例.

任何人都可以提供与处理它的在线资源的链接吗?书籍也欢迎,最好免费.Google搜索没有带来任何价值.

谢谢!

c++ boost stl unordered-set c++11

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

比较两个无序集合的平等有多贵?

给定两个std::sets,可以简单地同时迭代两个集合并比较元素,从而产生线性复杂性.这不适用于std::unordered_sets,因为元素可以按任何顺序存储.因此,如何昂贵的是a == bstd::unordered_set

c++ complexity-theory equality set unordered-set

9
推荐指数
2
解决办法
4940
查看次数

在Visual C++和clang中使用C++ 11 unordered_set

我试图在跨平台的C++应用程序中使用std :: unordered_set.它在Windows下编译和工作就像Visual C++中的魅力,但在Mac OS X下的clang上产生致命的编译错误.

我想知道它为什么会发生,以及正确的方法是什么.

示例代码:

// 
// Clang build cmdline:
// $ clang++ ./set.cpp -Wall -Werror -Wfatal-errors -std=c++11 -stdlib=libc++ -o set.out
// 

#include <iostream>
#include <unordered_set>

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }
    bool operator==(Point const& p) const {
        return this->x == p.x && this->y == p.y;
    }
    operator std::size_t () const {
        return std::hash<int>()(x) ^ std::hash<int>()(y);
    }
};

typedef std::unordered_set<Point> points_set_t; …
Run Code Online (Sandbox Code Playgroud)

c++ clang unordered-set visual-c++ c++11

9
推荐指数
1
解决办法
7802
查看次数

unordered_set非const迭代器

出于测试目的,我创建了一个小的unordered_set并尝试迭代该集合.该集合拥有自己的类:

class Student {
private:
    int matrNr;
    string name;
public:
    Student( const int& matrNr = 0, const string& name = "" )
        : matrNr( matrNr ), name( name ) {}
    void setNr( const int& matrNr ) {
        this->matrNr = matrNr;
    }
...
};
Run Code Online (Sandbox Code Playgroud)

我插入了一些元素并尝试在迭代期间更改对象:

unordered_set<Student, meinHash> meineHashTable;
meineHashTable.emplace( 12, "Fred" );
meineHashTable.emplace( 22, "Barney" );
meineHashTable.emplace( 33, "Wilma" );

for (int i = 0; i < meineHashTable.bucket_count(); i++) {
    cout << "Bucketnummer: " << i << endl;
    unordered_set<Student, meinHash>::local_iterator iter; …
Run Code Online (Sandbox Code Playgroud)

c++ stl unordered-set c++11

9
推荐指数
2
解决办法
4295
查看次数

调用 'unordered_set&lt; vector&lt;int&gt; &gt;' 隐式删除的默认构造函数

似乎当我尝试定义向量的 unordered_set 时,我收到一条错误消息:“调用隐式删除的默认构造函数unordered_set< vector<int> >。” 当我定义常规(有序)集时不会发生这种情况:set< vector<int> >。似乎我需要定义hash<vector<int>>才能摆脱错误。

有谁知道为什么我只有在使用时才会收到此错误unordered_set?不应该两个数据结构都使用散列,那么为什么 unordered_set 需要自定义散列函数?事实上,常规(有序)不应该也set需要一些自定义的比较器来对vector<int>数据结构进行排序吗?

c++ hash set unordered-set

9
推荐指数
2
解决办法
9615
查看次数

相当于C++的无序集的python的set.pop()

C++是否具有与python相同的功能set.pop()?我一直在寻找的文件unordered_set小号 在这里,但似乎没有成为一个办法1.访问任意元素,和/或2.访问+删除任意元素(膨化).

c++ python set unordered-set

8
推荐指数
2
解决办法
3153
查看次数

在一个只有const shared_ptr的unordered_set中找到一个shared_ptr?

我有一个unordered_set<shared_ptr<T>> us,我想知道针k是否在us,但k有类型shared_ptr<T const>所以unordered_set<shared_ptr<T>>::find抱怨它无法转换.

有没有解决的办法?也许通过直接提供哈希?

我确实尝试过const_cast(感觉很脏),但没有削减它.

c++ const unordered-set

8
推荐指数
1
解决办法
132
查看次数

使用无序对作为其键的 C# 字典?

我正在尝试创建一个 C# 字典,它将一对无序索引作为其键。

例如:

exampleDictionary[new UnorderedPair(x,y)]并且exampleDictionary[new UnorderedPair(y,x)]应该都返回相同的值。

除了使用 HashSet 之外,还有其他方法可以创建自定义无序集合吗?或者某种创建无序元组的方法?

这个问题与我想要完成的问题类似,只不过是在 C# 而不是 python 中。

c# dictionary unity-game-engine unordered-set unordered

8
推荐指数
2
解决办法
336
查看次数