unordered-map

What is the complexity of std::vector<T>::clear() when T is a primitive type?

橙三吉。 提交于 2019-11-29 09:17:59
I understand that the complexity of the clear() operation is linear in the size of the container, because the destructors must be called. But what about primitive types (and POD)? It seems the best thing to do would be to set the vector size to 0, so that the complexity is constant. If this is possible, is it also possible for std::unordered_map? dasblinkenlight It seems the best thing to do would be to set the vector size to 0, so that the complexity is constant. In general, the complexity of resizing a vector to zero is linear in the number of elements currently stored in the vector .

Defining custom hash function and equality function for unordered_map

浪尽此生 提交于 2019-11-29 08:33:28
问题 I am trying to define a type of unordered_map that has a custom hash function and equality comparison function. The function prototypes of these functions are as follows: //set<Vertex3DXT*> is the type of the key; Cell3DXT* is the type of the value size_t VertexSetHashFunction(set<Vertex3DXT*> vertexSet); //hash function bool SetEqual(set<Vertex3DXT*> a, set<Vertex3DXT*> b); //equality I have these function prototypes declared and then I try to declare the type as follows: typedef std::tr1:

Is an unordered_map really faster than a map in practice?

萝らか妹 提交于 2019-11-29 06:38:14
问题 Sure, the lookup performance of an unordered_map is constant on average, and the lookup performance of a map is O(logN). But of course in order to find an object in an unordered_map, we have to: hash the key we want to find. equality_compare the key with every key in the same bucket. Whereas in a map, we simply need to less_than compare the sought key with log2(N) keys, where N is the number of items in the map. I wondered what the real performance difference would be, given that the hash

Hash function for user defined class. How to make friends? :)

北慕城南 提交于 2019-11-29 06:01:42
I have a class C, which has a string* ps private data member. Now, I'd like to have an unordered_map<C, int> for which I need a custom hash function. According to the c++ reference , I can do that like namespace std { template<> class hash<C> { public: size_t operator()(const C &c) const { return std::hash<std::string>()(*c.ps); } }; } The problem is that I can't seem to make operator() and C friends so that I could access ps . I have tried this: class C; template<> class std::hash<C>; class C{ //... friend std::hash<C>::operator ()(const C&) const; // error: Incomplete type }; // define hash

Sorting std::unordered_map by key

↘锁芯ラ 提交于 2019-11-29 03:48:44
How can I sort an unordered_map by key? I need to print an unordered_map sorted by key. std::unordered_map<int, int> unordered; std::map<int, int> ordered(unordered.begin(), unordered.end()); for(auto it = ordered.begin(); it != ordered.end(); ++it) std::cout << it->second; An alternate solution is to construct a vector of the keys, sort the vector, and print per that sorted vector. This will be considerably faster than the approaches that constructed a map from the ordered map, but will also involve more code. std::unordered_map<KeyType, MapType> unordered; std::vector<KeyType> keys; keys

unordered_multimap - iterating the result of find() yields elements with different value

无人久伴 提交于 2019-11-28 21:26:52
The multimap in C++ seems to work really odd, i would like to know why #include <iostream> #include <unordered_map> using namespace std; typedef unordered_multimap<char,int> MyMap; int main(int argc, char **argv) { MyMap map; map.insert(MyMap::value_type('a', 1)); map.insert(MyMap::value_type('b', 2)); map.insert(MyMap::value_type('c', 3)); map.insert(MyMap::value_type('d', 4)); map.insert(MyMap::value_type('a', 7)); map.insert(MyMap::value_type('b', 18)); for(auto it = map.begin(); it != map.end(); it++) { cout << it->first << '\t'; cout << it->second << endl; } cout << "all values to a" <<

std::unordered_map::find using a type different than the Key type?

风流意气都作罢 提交于 2019-11-28 19:13:24
I have an unordered_map that uses a string-type as a key: std::unordered_map<string, value> map; A std::hash specialization is provided for string , as well as a suitable operator== . Now I also have a "string view" class, which is a weak pointer into an existing string, avoiding heap allocations: class string_view { string *data; size_t begin, len; // ... }; Now I'd like to be able to check if a key exists in the map using a string_view object. Unfortunately, std::unordered_map::find takes a Key argument, not a generic T argument. (Sure, I can "promote" one to a string , but that causes an

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

Difference between hash_map and unordered_map?

徘徊边缘 提交于 2019-11-28 16:58:35
I recently discovered that the implementation of the hash map in C++ will be called unordered_map . When I looked up why they weren't just using hash_map , I discovered that apparently there are compatibility issues with the implementation of hash_map that unordered_map resolves (more about it here ). That wiki page doesn't give much more information so I wondering if anyone knew some of the issues with hash_map that unordered_map resolves. Stef Since there was no hash table defined in the C++ standard library, different implementors of the standard libraries would provide a non-standard hash

What is the difference between unordered_map :: emplace and unordered_map :: insert in C++?

蹲街弑〆低调 提交于 2019-11-28 16:29:51
问题 What is the difference between std::unordered_map::emplace and std::unordered_map::insert in C++? 回答1: unordered_map::insert copies or moves a key-value pair into the container. It is overloaded to accept reference-to-const or an rvalue reference: std::pair<iterator,bool> insert(const std::pair<const Key, T>& value); template<class P> std::pair<iterator,bool> insert(P&& value); unordered_map::emplace allows you to avoid unnecessary copies or moves by constructing the element in place. It uses