unordered-set

Using mutable to allow modification of object in unordered_set

怎甘沉沦 提交于 2021-02-20 02:13:33
问题 Please consider the following code: #include <iostream> #include <unordered_set> struct MyStruct { int x, y; double mutable z; MyStruct(int x, int y) : x{ x }, y{ y }, z{ 0.0 } { } }; struct MyStructHash { inline size_t operator()(MyStruct const &s) const { size_t ret = s.x; ret *= 2654435761U; return ret ^ s.y; } }; struct MyStructEqual { inline bool operator()(MyStruct const &s1, MyStruct const &s2) const { return s1.x == s2.x && s1.y == s2.y; } }; int main() { std::unordered_set<MyStruct,

Is the order of two same unordered_maps the same?

隐身守侯 提交于 2021-02-07 18:41:44
问题 In other words, if I fill two unordered_map , or unordered_set , objects with exactly the same content and the same hashing function, will iterating over them give the same sequence of key/value pairs? If so, then what are the conditions for this to hold (e.g. same hashing function, same keys, not necessarily same values). 回答1: No. There is no requirement, for example, that objects that have the same hash be placed in any particular order. In fact, in general it's impossible for an unordered

Can not compare std::unorded_set with custom KeyEqual

烈酒焚心 提交于 2021-02-07 06:12:09
问题 The following program does not compile. But If I do not comment out operator== , it compiles. Why operator== is still needed when I already provide FooEqual #include <cstddef> #include <unordered_set> struct Foo { }; struct FooHasher { size_t operator()(const Foo&) const { return 1; } }; struct FooEqual { bool operator()(const Foo& lhs, const Foo& rhs) const { return true; } }; // bool operator==(const Foo& lhs, const Foo& rhs) { // return true; // } int main() { std::unordered_set<Foo,

Can not compare std::unorded_set with custom KeyEqual

强颜欢笑 提交于 2021-02-07 06:12:01
问题 The following program does not compile. But If I do not comment out operator== , it compiles. Why operator== is still needed when I already provide FooEqual #include <cstddef> #include <unordered_set> struct Foo { }; struct FooHasher { size_t operator()(const Foo&) const { return 1; } }; struct FooEqual { bool operator()(const Foo& lhs, const Foo& rhs) const { return true; } }; // bool operator==(const Foo& lhs, const Foo& rhs) { // return true; // } int main() { std::unordered_set<Foo,

Remove element in unordered_set using iterator in a loop

别说谁变了你拦得住时间么 提交于 2021-01-27 15:07:08
问题 Please consider the following code: Class MyClass is a self-defined class: class MyClass { public: MyClass(int v) : Val(v) {} int Val; }; Then the following code will cause Debug Assertion Failed in the loop just after calling it = T.erase(it); : unordered_set<MyClass*> T; unordered_set<MyClass*>::iterator it; for (int i=0; i<10; i++) T.insert(new MyClass(i)); for (it = T.begin(); it != T.end(); it++) { if ( (*it)->Val == 5 ) it = T.erase(it); // After this line executes, in the next loop,

Why does the 32769th insert fail in std::unordered_set?

泪湿孤枕 提交于 2021-01-27 15:06:34
问题 I generate a large number of class instances and store them in a std::unordered_set . I have defined a hash function and an equality relation, and so far everything works as it should - I insert 10000 instances with unordered_set::insert , and I can find them with unordered_set::find . All the objects are undamaged, and there is no hint on memory corruption or any other issue. However, when I keep inserting, the 32769th insert fails - it doesn't throw, but it returns a pair where the iterator

creating unordered_set with lambda

点点圈 提交于 2020-12-29 10:12:34
问题 How can I make unordered_set with lambda? (I know how to make it with user defined hash struct and operator== ) My current code is : #include <unordered_set> #include <functional> struct Point { float x; float y; Point() : x(0), y(0) {} }; int main() { auto hash=[](const Point& pt){ return (size_t)(pt.x*100 + pt.y); }; auto hashFunc=[&hash](){ return std::function<size_t(const Point&)> (hash); }; auto equal=[](const Point& pt1, const Point& pt2){ return ((pt1.x == pt2.x) && (pt1.y == pt2.y));

Efficiently erase a unique_ptr from an unordered_set

孤人 提交于 2020-04-11 05:47:07
问题 I am storing the ownership of some objects inside an unordered_set , using unique_ptr s. But I don't know a good way to erase one of them from the set, when the time comes. Code looks something like this: typedef unique_ptr<MyType> MyPtr; unordered_set<MyPtr> owner; MyPtr p = make_unique<MyType>("foo") MyType *pRaw = p.get(); owner.insert(std::move(p)); // Later ... // I want to do something like this (cannot be written as-is, of course): // owner.erase(pRaw); Is there a way to do this? I can

Efficiently erase a unique_ptr from an unordered_set

时光总嘲笑我的痴心妄想 提交于 2020-04-11 05:44:14
问题 I am storing the ownership of some objects inside an unordered_set , using unique_ptr s. But I don't know a good way to erase one of them from the set, when the time comes. Code looks something like this: typedef unique_ptr<MyType> MyPtr; unordered_set<MyPtr> owner; MyPtr p = make_unique<MyType>("foo") MyType *pRaw = p.get(); owner.insert(std::move(p)); // Later ... // I want to do something like this (cannot be written as-is, of course): // owner.erase(pRaw); Is there a way to do this? I can

How can I use an unordered_set with a custom struct?

无人久伴 提交于 2020-01-21 04:48:25
问题 I want to use an unordered_set with a custom struct . In my case, the custom struct represents a 2D point in an euclidean plane. I know that one should define a hash function and comparator operator and I have done so as you can see in my code below: struct Point { int X; int Y; Point() : X(0), Y(0) {}; Point(const int& x, const int& y) : X(x), Y(y) {}; Point(const IPoint& other){ X = other.X; Y = other.Y; }; Point& operator=(const Point& other) { X = other.X; Y = other.Y; return *this; };