unordered-map

Calculating memory space occupied by unordered maps

99封情书 提交于 2019-12-11 02:54:37
问题 I have two unordered maps: (code is executed in linux) First unordered map: It consists of more atleast 65536 entries. Each entry consists of int unsigned char unsigned char Second unordered map: It consists of less than 65536 enteries. Each entry consist of int int int vector <char> Now I want to make comparison of the two on the basis of memory occupied by the above two unordered maps (in bytes). After that I want to calculate memory compression achieved. Please guide me how can I find the

Help understanding segfault with std::map/boost::unordered_map

对着背影说爱祢 提交于 2019-12-11 02:33:30
问题 I have some code to handle resource (images, fonts, mesh data, etc.) management using a template'd static class, allowing client code to do something like: ResourceManager<Texture>::init("data/textures"); ResourceManager<Font>::init("data/fonts"); // later ... boost::shared_ptr<const Texture> tex = ResourceManager<Texture>::getResource("wall.png"); boost::shared_ptr<const Font> font = ResourceManager<Font>::getResource("Arial.ttf"); // later ... ResourceManager<Texture>::release(); The

Creating an unordered_map of std::functions with any arguments?

北城余情 提交于 2019-12-11 01:05:33
问题 I am trying to create a unordered map of std::functions . Where a key is a string where you will look up the function you want to call and the function is the value. I have written a small program: #include <iostream> #include <unordered_map> #include <functional> #include <string> void func1() { std::cout << "Function1 has been called." << std::endl; } int doMaths(int a) { return a + 10; } int main() { std::unordered_map<std::string,std::function<void()>> myMap; myMap["func1"] = func1; }

Unordered_map iterator invalidation

孤人 提交于 2019-12-11 00:34:48
问题 i have this iterator loop, typedef boost::unordered_map<std::pair<int, int>, NavigationNode> NodesMap; NodesMap nodes; for (NodesMap::iterator it= nodes.begin(); it != nodes.end() ; ++it) { if(it->second.type == NavigationNodeType_Walkable) { ConnectNode(&it->second); } } ConnectNode function seems to be invalidating the iterator. It pushes new elements inside the NavigationNode and modifies existing members of the NavigationNode. i have two questions Is passing it->second as pointer bad?

Two dimensional unordered_map

人走茶凉 提交于 2019-12-10 21:34:42
问题 typedef boost::unordered_map<int, void*> OneDimentionalNodes; typedef boost::unordered_map<int, OneDimentionalNodes> TwoDimentionalNodes; TwoDimentionalNodes nodes; is this valid? i don't use any hash functions since keys of the unordered_maps' are single integers. it compiles, but when i iterate it like this, it crashes while trying to access this->hash_function()(k); for (TwoDimentionalNodes::iterator it= nodes.begin(); it != nodes.end() ; ++it) { for(OneDimentionalNodes::iterator it2 =

Why hash_map and unordered_map on my machine are extremely slow?

点点圈 提交于 2019-12-10 16:37:51
问题 I tested them with this code (on Visual Studio 2010 sp1): #include <ctime> #include <iostream> #include <map> #include <unordered_map> #include <hash_map> int main() { clock_t time; int LOOP = (1 << 16); std::map<int, int> my_map; std::unordered_map<int, int> map_unordered_map; std::hash_map<int, int> my_hash_map; time = clock(); for (int i = 0; i != LOOP; ++i) { my_map[i] = i; } std::cout << "map: " << ((double)(clock() - time) / CLOCKS_PER_SEC) << std::endl; time = clock(); for (int i = 0;

Order of unordered_map changes on assignment

徘徊边缘 提交于 2019-12-10 16:00:58
问题 I'm curious about this behaviour. I found that assigning an unordered_map changes the internal order of the unordered map, without any insertion/deletion: unordered_map<int, string> m1; unordered_map<int, string> m2; unordered_map<int, string> m3; m1[2] = "john"; m1[4] = "sarah"; m1[1] = "mark"; m2 = m1; m3 = m2; for(auto it = m1.begin(); it != m1.end(); ++it) { cout << it->second << " "; } cout << endl; for(auto it = m2.begin(); it != m2.end(); ++it) { cout << it->second << " "; } cout <<

unordered_map to have three elements

自闭症网瘾萝莉.ら 提交于 2019-12-10 13:56:08
问题 I am trying to have three elements in an unordered_map . I tried the following code #include <iostream> #include <string> #include <algorithm> #include <boost/unordered_map.hpp> typedef boost::unordered_map<int, std::pair<int, int>> mymap; mymap m; int main(){ //std::unordered_map<int, std::pair<int, int> > m; m.insert({3, std::make_pair(1,1)}); m.insert({4, std::make_pair(5,1)}); for (auto& x: m) std::cout << x.first << ": "<< x.second << std::endl; } but I get many errors in the print

Using an unordered_map where Key is a member of T

蹲街弑〆低调 提交于 2019-12-10 13:49:59
问题 Is there any nice way to use an unordered_map so that you can access objects by a member variable in constant time (average case)? The following example has this functionality but requires the name of each Person to be duplicated as the Key: #include <iostream> #include <string> #include <unordered_map> #include <algorithm> class Person { public: Person() : name_("") {} Person(const std::string& name) : name_(name) {} std::string getName() const { return name_; } void kill() const { std::cout

How can I use a custom type for keys in a boost::unordered_map?

荒凉一梦 提交于 2019-12-10 11:17:16
问题 I'm using Boost's implementation of a hash map in a project right now, and I'm trying to implement a custom type for keys. I have four unsigned integers which I'd like to combine into a single 128-bit datatype to use as a key. I've created a struct with a 32-bit integer array of four elements, which serves as my storage. To be honest, I'm not sure how Boost's hash map works, so I'm not sure what I'm doing here, but I followed the Boost documentation (http://www.boost.org/doc/libs/1_37_0/doc