unordered-set

How can I use a C++ unordered_set for a custom class?

。_饼干妹妹 提交于 2019-12-04 12:02:40
问题 How can I store objects of a class in an unordered_set ? My program needs to frequently check if an object exists in this unordered_set and if it does, then do some update on that object. I have looked up online on how to use unordered_set , but sadly most tutorials are about using it on int or string types. But how can I use it on a class? How can I define a hash function to make the node_id in the following example the key of the unordered_set ? #include <iostream> #include <unordered_set>

adding elements of a vector to an unordered set

谁说胖子不能爱 提交于 2019-12-03 22:24:58
Is there an easy way to add all the elements of a vector to an unordered_set ? They are of the same type. Right now, I am using a for loop and was wondering if there is a better way to do it If you're constructing the unordered_set then: std::vector<int> v; std::unordered_set<int> s(v.begin(), v.end()); Forgive me if my syntax has any minor bugs, but you can try the std::copy function , its meant for this purpose. std::vector<int> v; std::unordered_set<int> s; std::copy(v.begin(),v.end(),std::inserter(s,s.end())); 来源: https://stackoverflow.com/questions/12850927/adding-elements-of-a-vector-to

set vs unordered_set for fastest iteration

孤者浪人 提交于 2019-12-03 10:39:56
In my application I have the following requirements - The data structure will be populated just once with some values (not key/value pairs). The values may be repeated but I want the data structure to store them once only. I will be iterating 100s of times through all the elements of the data structure created above. The order in which the elements appear in the iteration is immaterial. Constraint 1 suggests that I will either have to use set or unordered_set as data is not in the form of key value pairs. Now set insertion is costlier than unordered_set insertion but the data structure is

How can I use a C++ unordered_set for a custom class?

扶醉桌前 提交于 2019-12-03 06:48:34
How can I store objects of a class in an unordered_set ? My program needs to frequently check if an object exists in this unordered_set and if it does, then do some update on that object. I have looked up online on how to use unordered_set , but sadly most tutorials are about using it on int or string types. But how can I use it on a class? How can I define a hash function to make the node_id in the following example the key of the unordered_set ? #include <iostream> #include <unordered_set> using namespace std; // How can I define a hash function that makes 'node' use 'node_id' as key? struct

Sorting on unordered_sets

大兔子大兔子 提交于 2019-12-02 04:51:10
问题 I have a list of items that are created each frame and need to be sorted. Each Item's first member variable to sort by is an unordered_set . I've moved this to an ordered set everywhere in the system so I can sort it in the list of items. But I'm suffering a performance hit in another are of the code foe this. Bearing in mind that each item will be destroyed and be recreated on a per-frame basis, is there anything I can do to hold these in unordered_set s and sort them? class item { public:

Why STL unordered_map and unordered_set cannot be sorted by STL algorithms?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 18:49:44
I'll start by illustrating a simple use case example: Consider the problem of a social security ID database, where in C++ code is modelled as a std::unordered_map where its key is the social security ID of a person and its value is a std::string with the full-name of that person (e.g., std::unordered_map<int, std::string> DB; ). Consider also, that there's a request for printing this database sorted in ascending order based on the person's ID (i.e., std::unordered_map 's key). Naively, one would think to use std::sort in order to sort the std::unordered_map according to the requested criteria

Why STL unordered_map and unordered_set cannot be sorted by STL algorithms?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 17:37:47
问题 I'll start by illustrating a simple use case example: Consider the problem of a social security ID database, where in C++ code is modelled as a std::unordered_map where its key is the social security ID of a person and its value is a std::string with the full-name of that person (e.g., std::unordered_map<int, std::string> DB; ). Consider also, that there's a request for printing this database sorted in ascending order based on the person's ID (i.e., std::unordered_map 's key). Naively, one

unordered set intersection in C++

冷暖自知 提交于 2019-12-01 05:14:33
Here is my code, wondering any ideas to make it faster? My implementation is brute force, which is for any elements in a, try to find if it also in b, if so, put in result set c. Any smarter ideas is appreciated. #include <iostream> #include <unordered_set> int main() { std::unordered_set<int> a = {1,2,3,4,5}; std::unordered_set<int> b = {3,4,5,6,7}; std::unordered_set<int> c; for (auto i = a.begin(); i != a.end(); i++) { if (b.find(*i) != b.end()) c.insert(*i); } for (int v : c) { std::printf("%d \n", v); } } Asymptotically, your algorithm is as good as it can get. In practice, I'd add a

How to make a c++11 std::unordered_set of std::weak_ptr

回眸只為那壹抹淺笑 提交于 2019-12-01 03:39:35
I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName; It works fine. But I would like to change it to an unordered set. However, I get about six pages of errors when I do that. Any ideas how to do that? After looking through all the pages of error messages I found to lines that might help. /usr/include/c++/4.7/bits/functional_hash.h:60:7: error: static assertion failed: std::hash is not specialized for this type /usr/include/c++/4.7/bits/stl_function.h: In instantiation of ‘bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::weak_ptr

unordered set intersection in C++

为君一笑 提交于 2019-12-01 01:59:01
问题 Here is my code, wondering any ideas to make it faster? My implementation is brute force, which is for any elements in a, try to find if it also in b, if so, put in result set c. Any smarter ideas is appreciated. #include <iostream> #include <unordered_set> int main() { std::unordered_set<int> a = {1,2,3,4,5}; std::unordered_set<int> b = {3,4,5,6,7}; std::unordered_set<int> c; for (auto i = a.begin(); i != a.end(); i++) { if (b.find(*i) != b.end()) c.insert(*i); } for (int v : c) { std: