unordered-map

C++ Hash function for string in unordered_map

别来无恙 提交于 2019-12-03 11:31:44
问题 It seems as if C++ does not have a hash function for strings in the standard library. Is this true? What is a working example of using a string as a key in an unordered_map that will work with any c++ compiler? 回答1: C++ STL provides template specializations of std::hash for the various string classes. You could just specify std::string as key type for std::unordered_map : #include <string> #include <unordered_map> int main() { std::unordered_map<std::string, int> map; map["string"] = 10;

unordered_map: which one is faster find() or count()?

眉间皱痕 提交于 2019-12-03 08:29:50
问题 What is the fastest way to figure out if an unordered_map container has an item with a specified key? 回答1: They will have about equal performance. You should use the algorithm that best expresses what you are trying to do. To elaborate on that, generally count() will be implemented using find() . For example, in libcxx, count() is implemented as return (find(__k) != end()); 回答2: find() and count() are applicable to many containers in C++. For maps, sets etc. find will always have constant

Defining a hash function in TR1 unordered_map inside a struct

£可爱£侵袭症+ 提交于 2019-12-03 03:48:55
According to this , it is possible to define an equality function in a TR1 unordered_map like this: #include <tr1/unordered_map> using namespace std; using namespace std::tr1; struct foo{ ... bool operator==(const foo& b) const{ return ..; } }; unordered_map<foo,int> map; Is it possible to define the hash function the same way? If you want to change the default hashing (or, more often, provide hashing for a type that isn't currently supported), you provide a specialization of std::tr1::hash<T> for your key-type: namespace std { namespace tr1 { template<> struct hash<typename my_key_type> { std

C++ Hash function for string in unordered_map

喜夏-厌秋 提交于 2019-12-03 01:03:12
It seems as if C++ does not have a hash function for strings in the standard library. Is this true? What is a working example of using a string as a key in an unordered_map that will work with any c++ compiler? C++ STL provides template specializations of std::hash for the various string classes. You could just specify std::string as key type for std::unordered_map : #include <string> #include <unordered_map> int main() { std::unordered_map<std::string, int> map; map["string"] = 10; return 0; } Dave I ran into this today (actually with wstring , not string , but it's the same deal): using

Obtaining list of keys and values from unordered_map

时间秒杀一切 提交于 2019-12-02 18:42:41
What is the most efficient way of obtaining lists (as a vector ) of the keys and values from an unordered_map ? For concreteness, suppose the map in question is a unordered_map<string, double> . I'd then like to obtain the keys as a vector<string> , and the values as a vector<double> . unordered_map<string, double> um; vector<string> vs = um.enum_keys(); vector<double> vd = um.enum_values(); I can just iterate across the map and collect the result, but is there a more efficient method? It would be nice to have a method that also works for regular map, since I might switch to that. Keith Layne

Building an unordered map with tuples as keys

陌路散爱 提交于 2019-12-02 18:09:28
In a C++ program with Boost, I am trying to build an unordered map whose keys are tuples of doubles: typedef boost::tuples::tuple<double, double, double, double> Edge; typedef boost::unordered_map< Edge, int > EdgeMap; Initializing the map completes OK, however, when I try to populate it with keys and values EdgeMap map; Edge key (0.0, 0.1, 1.1, 1.1); map[key] = 1; I encounter the following error message: /usr/include/boost/functional/hash/extensions.hpp:176: error: no matching function for call to ‘hash_value(const boost::tuples::tuple<double, double, double, double, boost::tuples::null_type,

Using The [] Operator Efficiently With C++ unordered_map

孤街醉人 提交于 2019-12-02 17:03:38
Firstly could someone clarify whether in C++ the use of the [] operator in conjunction with an unordered_map for lookups wraps a call to the find() method, or is using the [] operator quicker than find()? Secondly, in the following piece of code I suspect in cases where the key is not already in the unordered_map I am performing a second look up by way of the line map[key] = value in order to replace the default value created there by using the [] operator when a key is not present. Is that true, and if so is there a way (perhaps by use of pointers or something) that I might only perform one

static unordered_map is erased when putting into different compilation unit in XCode

旧时模样 提交于 2019-12-02 04:33:31
I have a static unordered_map in my class C. I experience difference in behaviour if I put my class definition and declaration in different files from the file containing function main. The thing is that I observed that if the class C is in the same compilation unit as function main, all is well, I see only once the text "new string created: c". However if I split my code into three files (see the listing below), I see "new string created: c" twice which means that my static unordered_map is wiped right before entering main . My question would be: why does this happen? ( The difference only

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

C++ unordered_map<string, …> lookup without constructing string

99封情书 提交于 2019-12-01 18:19:59
I have C++ code that investigates a BIG string and matches lots of substrings. As much as possible, I avoid constructing std::strings, by encoding substrings like this: char* buffer, size_t bufferSize At some point, however, I'd like to look up a substring in one of these: std::unordered_map<std::string, Info> stringToInfo = {... So, to do that, I go: stringToInfo.find(std::string(buffer, bufferSize)) That constructs a std::string for the sole purpose of the lookup. I feel like there's an optimization I could do here, by... changing the key-type of the unordered_map to some kind of temporary