unordered-map

Using The [] Operator Efficiently With C++ unordered_map

眉间皱痕 提交于 2019-12-20 08:47:13
问题 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

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

こ雲淡風輕ζ 提交于 2019-12-20 03:53:06
问题 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

Unordered map: issue using class member function pointer

丶灬走出姿态 提交于 2019-12-19 11:58:08
问题 I have the following problem: I am writing a simple chip8 emulator, and have a massive class of interpreter functions that I would like to access via opcodes as keys, such as with a dictionary. That is to replace a massive switch case, and I understand that for that purpose, an unordered map is a nice tool to use. This approach works easily with just functions (because of their static scope, I assume), but does not with classes, because of the same concept of scope. I am somewhat new to

C++ some questions on boost::unordered_map & boost::hash

不打扰是莪最后的温柔 提交于 2019-12-19 06:25:12
问题 I've only recently started dwelling into boost and it's containers, and I read a few articles on the web and on stackoverflow that a boost::unordered_map is the fastest performing container for big collections. So, I have this class State, which must be unique in the container (no duplicates) and there will be millions if not billions of states in the container. Therefore I have been trying to optimize it for small size and as few computations as possible. I was using a boost::ptr_vector

How to measure Memory Usage of std::unordered_map

五迷三道 提交于 2019-12-18 18:54:39
问题 We know that hash table based container implementations like std::unordered_map use a lot memory but I don't know how much is how much? Apart from space complexity notations, and not considering if a container element is a pointer to a larger object: Is there any way to figure out how many bytes is being used by such a container during run-time? Is there a way to tell, at runtime, how much memory any container uses? 回答1: If you want to get a rough size, I think bucket_count() and max_load

Using a std::tuple as key for std::unordered_map

耗尽温柔 提交于 2019-12-18 12:26:49
问题 With the code below, I get a very confusing error in MSVC that seems to suggest the key type (an std::tuple) is being converted to an std::string. #include <iostream> #include <string> #include <tuple> #include <utility> #include <unordered_map> typedef std::tuple<std::string,int,char> key_t; struct key_hash : public std::unary_function<key_t, std::size_t> { std::size_t operator()(const key_t& k) const { return std::get<0>(k)[0] ^ std::get<1>(k) ^ std::get<2>(k); } }; struct key_equal :

Using Boost unordered_map

本秂侑毒 提交于 2019-12-18 07:03:44
问题 I want to include boost::unordered_map in my project without downloading the whole Boost package. How can I do that? 回答1: Use bcp : http://www.boost.org/doc/libs/1_52_0/tools/bcp/doc/html/index.html cd $BOOST_DIR bcp unordered_map /tmp/TEST Now /tmp/TEST contains only the things required for unordered_map , in my case 15Mb (as opposed to 734Mb for the full boost library) 回答2: You need at least headers because Boost packages depend on each other. You might want to select only needed header

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

有些话、适合烂在心里 提交于 2019-12-18 04:35:20
问题 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? 回答1: 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

How do I implement a CString hash function for use with std::unordered_map?

孤人 提交于 2019-12-17 20:49:40
问题 I want to declare : std::unordered_map<CString, CString> m_mapMyMap; But when I build I got an error telling me that the standard C++ doesn't provide a hash function for CString, while CString have the (LPCSTR) operator. How do I properly implement a hash function for CString? 回答1: Based on the MS STL implementation for std::string I created the following methods which can be used for std::unordered_set and std::unordered_map : namespace std { template <> struct hash<CString> { // hash

What is the default hash function used in C++ std::unordered_map?

蓝咒 提交于 2019-12-17 08:47:52
问题 I am using unordered_map<string, int> and unordered_map<int, int> What hash function is used in each case and what is chance of collision in each case? I will be inserting unique string and unique int as keys in each case respectively. I am interested in knowing the algorithm of hash function in case of string and int keys and their collision stats. 回答1: The function object std::hash<> is used. Standard specializations exist for all built-in types, and some other standard library types such