unordered-set

Is there a default hash function for an unordered_set of a custom class?

假装没事ソ 提交于 2019-11-27 23:12:05
I'm using a std::unordered_set for the first time and have a question about the hash function. As far as I understand, if you don't specify a hash function it will default to std::hash<Key> . I have a mySet member in one of my classes: typedef std::unordered_set<MyClass> USetType; USetType mySet; When I try to build, I get the following error: error C2440: 'type cast' : cannot convert from 'const MyClass' to 'size_t' Is it necessary to define a conversion function (to size_t ) if you want to use unordered_set with a custom class? Is there any way to avoid writing your own hash function and

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

China☆狼群 提交于 2019-11-27 16:25: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

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

倾然丶 夕夏残阳落幕 提交于 2019-11-27 11:43:48
问题 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

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

前提是你 提交于 2019-11-27 05:35:07
问题 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()' 回答1: Your code compiles on VS2010 SP1 (VC10), but it fails to compile with GCC g++ 4.7.2.

Inserting into an unordered_set with custom hash function

≡放荡痞女 提交于 2019-11-27 04:30:57
I have the following code to make an unordered_set<Interval> . This compiles fine. 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

Is there a default hash function for an unordered_set of a custom class?

爷,独闯天下 提交于 2019-11-26 21:21:23
问题 I'm using a std::unordered_set for the first time and have a question about the hash function. As far as I understand, if you don't specify a hash function it will default to std::hash<Key> . I have a mySet member in one of my classes: typedef std::unordered_set<MyClass> USetType; USetType mySet; When I try to build, I get the following error: error C2440: 'type cast' : cannot convert from 'const MyClass' to 'size_t' Is it necessary to define a conversion function (to size_t ) if you want to

Generic hash for tuples in unordered_map / unordered_set

a 夏天 提交于 2019-11-26 14:35:33
Why doesn't std::unordered_map<tuple<int, int>, string> just work out of the box? It is tedious to have to define a hash function for tuple<int, int> , e.g. template<> struct do_hash<tuple<int, int>> { size_t operator()(std::tuple<int, int> const& tt) const {...} }; Building an unordered map with tuples as keys (Matthieu M.) shows how to automate this for boost::tuple . Is there anyway of doing this for c++0x tuples without using variadic templates? Surely this should be in the standard :( This works on gcc 4.5 allowing all c++0x tuples containing standard hashable types to be members of

How to specialize std::hash<Key>::operator() for user-defined type in unordered containers?

為{幸葍}努か 提交于 2019-11-26 11:16:54
To support user-defined key types in std::unordered_set<Key> and std::unordered_map<Key, Value> one has to provide operator==(Key, Key) and a hash functor: 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; It would be more convenient to write just std::unordered_set<X> with a default hash for type X , like for types coming along with the compiler and library. After consulting C++ Standard Draft N3242 §20.8.12 [unord.hash] and §17.6.3.4

Generic hash for tuples in unordered_map / unordered_set

≯℡__Kan透↙ 提交于 2019-11-26 03:57:00
问题 Why doesn\'t std::unordered_map<tuple<int, int>, string> just work out of the box? It is tedious to have to define a hash function for tuple<int, int> , e.g. template<> struct do_hash<tuple<int, int>> { size_t operator()(std::tuple<int, int> const& tt) const {...} }; Building an unordered map with tuples as keys (Matthieu M.) shows how to automate this for boost::tuple . Is there anyway of doing this for c++0x tuples without using variadic templates? Surely this should be in the standard :(

How to specialize std::hash<Key>::operator() for user-defined type in unordered containers?

江枫思渺然 提交于 2019-11-26 01:58:56
问题 To support user-defined key types in std::unordered_set<Key> and std::unordered_map<Key, Value> one has to provide operator==(Key, Key) and a hash functor: 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; It would be more convenient to write just std::unordered_set<X> with a default hash for type X , like for types coming along with the