unordered-set

How do I use unordered_set? [duplicate]

最后都变了- 提交于 2019-11-30 06:02:50
This question already has an answer here: Using C++11 unordered_set in Visual C++ and clang 1 answer I am trying to define an unordered_set like this: unordered_set<Point> m_Points; When I compile it, I get the following error: The C++ Standard doesn't provide a hash for this type. Class 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); } }

unordered_set non const iterator

痴心易碎 提交于 2019-11-30 03:13:47
问题 For testing purposes I created a little unordered_set and tried to iterate over the set. The set holds an own class: 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; } ... }; I inserted some elements and tried to change the objects during iteration: unordered_set<Student, meinHash> meineHashTable; meineHashTable.emplace( 12, "Fred" );

Are there no specializations of std::hash for standard containers?

假如想象 提交于 2019-11-30 00:16:04
问题 I just found myself a little bit surprised being unable to simply use a std::unordered_set<std::array<int, 16> > test; because there does not seem to be a std::hash specialization for std::array s. Why is that? Or did I simply not find it? If there is indeed none, can the following implementation attempt be simplified? 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

How to specialize std::hash for type from other library

你说的曾经没有我的故事 提交于 2019-11-29 15:34:22
So the library I use has an enum (say it's named LibEnum ). I need to have an std::unordered_set of LibEnum , but I get compilation error that there is no specialized std::hash for it. I could easily write it and just return the number of value (first element is 0, second 1 etc), but where exactly I should put this specialization and how should it look like? I can't modify the library sources. enum LibEnum { A, B, C, D}; std::unordered_set <LibEnum> mySet; //need std::hash for LibEnum //how should it look like? You can just specialise std::hash for your type: namespace std { template <> struct

Random element from unordered_set in O(1)

喜欢而已 提交于 2019-11-29 09:47:48
I've seen people mention that a random element can be grabbed from an unordered_set in O(1) time. I attempted to do so with this: std::unordered_set<TestObject*> test_set; //fill with data size_t index = rand() % test_set.size(); const TestObject* test = *(test_set.begin() + index); However, unordered_set iterators don't support + with an integer. begin can be given a size_t param, but it is the index of a bucket rather than an element. Randomly picking a bucket then randomly picking an element within it would result in a very unbalanced random distribution. What's the secret to proper O(1)

How do I use unordered_set? [duplicate]

六眼飞鱼酱① 提交于 2019-11-29 05:31:31
问题 This question already has an answer here : Using C++11 unordered_set in Visual C++ and clang (1 answer) Closed 6 years ago . I am trying to define an unordered_set like this: unordered_set<Point> m_Points; When I compile it, I get the following error: The C++ Standard doesn't provide a hash for this type. Class 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 ==

Using C++11 unordered_set in Visual C++ and clang

北城以北 提交于 2019-11-29 02:06:04
I'am trying to use std::unordered_set in cross-platform C++ application. It compiles and works like a charm in Visual C++ under Windows, but generates a fatal compilation error on clang under Mac OS X. I want to know why it happens and what is the right way to get this working. Example code: // // 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

How to use unordered_set that has elements that are vector of pair<int,int>

半腔热情 提交于 2019-11-28 18:52:32
I wanted to have something like unordered_set<vector<pair<int,int>>> us; but even without pair: #include <vector> #include <unordered_set> using namespace std; int main() { unordered_set<vector<int>> um; } it fails: In file included from /usr/include/c++/4.8/bits/hashtable.h:35:0, from /usr/include/c++/4.8/unordered_set:47, from prog.cpp:2: /usr/include/c++/4.8/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::_Hash_code_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default

How to specialize std::hash for type from other library

人盡茶涼 提交于 2019-11-28 09:35:36
问题 So the library I use has an enum (say it's named LibEnum ). I need to have an std::unordered_set of LibEnum , but I get compilation error that there is no specialized std::hash for it. I could easily write it and just return the number of value (first element is 0, second 1 etc), but where exactly I should put this specialization and how should it look like? I can't modify the library sources. enum LibEnum { A, B, C, D}; std::unordered_set <LibEnum> mySet; //need std::hash for LibEnum //how

How can I make an unordered set of pairs of integers in C++?

强颜欢笑 提交于 2019-11-28 06:12:56
The following program does not compile an unordered set of pairs of integers, but it does for integers. Can unordered_set and its member functions be used on user-defined types, and how can I define it? #include <unordered_set> ... class A{ ... private: std::unordered_set< std::pair<int, int> > u_edge_; }; Compiler error: error: no matching function for call to 'std::unordered_set >::unordered_set()' Your code compiles on VS2010 SP1 (VC10), but it fails to compile with GCC g++ 4.7.2. However, you may want to consider boost::hash from Boost.Functional to hash a std::pair (with this addition,