unordered-map

std::unordered_map::emplace object creation

守給你的承諾、 提交于 2020-02-03 05:16:19
问题 I was in the process of selecting one of two methods of putting things into an unordered_map: std::unordered_map<Key, Value> map; map.emplace( std::piecewise_construct, std::forward_as_tuple(a), std::forward_as_tuple(b, c, d)); vs std::unordered_map<Key, DifferentValue> map; auto& value = map[a]; if (value.isDefaultInitialized()) value = DifferentValue(b, c, d); I did some experiments to see which one would perform better to find that when inserting unique elements, the behaviour (as in

Python List of Dictionaries to C++

丶灬走出姿态 提交于 2020-01-23 21:38:25
问题 I have a Python list of dictionaries that I would like to translate to C++. My list looks more or less like this: myList = [{"v_A": 5, "v_B": 3}, {"v_A": 1, "v_B":2}, {"v_A":0, "v_B": 0}] My code is designed such that when I look for e.g. velocities of particle 0, I do: v = myList[0]["v_"+letter] where letter would be a string, "A" or "B" , depeding on the output of a sorting function I have. I have found this is the most convenient way to achieve my purpose in Python. I am trying to

boost::unordered_map missing reserve() like std::unordered_map

China☆狼群 提交于 2020-01-21 18:34:44
问题 For my next task I need to use a very big hash; since I have an old compiler I cannot use C++0x std::unordered_map . Ideally I need is a call to reserve to make room in advance for a large number of items. I cannot find this method in boost::unordered_map : is there any place or function that achieves the same? The 2 associative container are the same; I can see rehash function and the same constructor for controlling the number of buckets, but not a function about a number of elements. Can

Why is std::unordered_map slow, and can I use it more effectively to alleviate that?

南楼画角 提交于 2020-01-21 04:56:47
问题 I’ve recently found out an odd thing. It seems that calculating Collatz sequence lengths with no caching at all is over 2 times faster than using std::unordered_map to cache all elements. Note I did take hints from question Is gcc std::unordered_map implementation slow? If so - why? and I tried to used that knowledge to make std::unordered_map perform as well as I could (I used g++ 4.6, it did perform better than recent versions of g++, and I tried to specify a sound initial bucket count, I

Sorting std::unordered_map by key

耗尽温柔 提交于 2020-01-20 03:55:26
问题 How can I sort an unordered_map by key? I need to print an unordered_map sorted by key. 回答1: 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; 回答2: 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

How to set a value in an unordered_map and find out if a new key was added

本小妞迷上赌 提交于 2020-01-13 04:40:08
问题 How can I efficiently and idiomatically set a value in an unordered_map and find out if a new key was added: #include <unordered_map> #include <string> int main() { auto map = std::unordered_map<std::string, int>{{"foo", 1}, {"bar", 2}}; map["foo"] = 3; // how to find out if a new key was added? } I can't use insert() directly because I want to overwrite the value if there is one already and insert does not do that. I can't use operator[] directly because it provides no information about

Using Both Map and List for Same Objects

孤街浪徒 提交于 2020-01-11 11:48:27
问题 I'm trying to use both a list and an unordered_map to store the same set of objects. I'm new to C++, so still getting comfortable with iterators. Say I have the following test code: class Test { public: int x; int y; int z; Test (int, int, int); } Test t1 = Test(1,2,3); Test t2 = Test(2,4,6); Test t3 = Test(3,6,9); std::list<Test> list; std::unordered_map<int, Test> map; list.push_back(t3); list.push_back(t2); list.push_back(t1); map[101] = t1; map[102] = t2; map[103] = t3; Is it possible to

unordered_map with string in managed_shared_memory fails

☆樱花仙子☆ 提交于 2020-01-06 03:08:30
问题 This is my code: int main (int argc, char *argv[]) { typedef int KeyType; typedef string MappedType; typedef std::pair<KeyType, MappedType> ValueType; typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager> ShmAlloc; typedef boost::unordered_map<KeyType, MappedType, boost::hash<KeyType>, std::equal_to<KeyType>, ShmAlloc> ShmHashMap; boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create,

Different behaviour with std::unordered_map container on Windows and Linux

别等时光非礼了梦想. 提交于 2020-01-05 08:39:58
问题 For my program I need to have unordered key. To do the job done I use std::unordered_map container. Here's a test code : #include <iostream> #include <unordered_map> #include <string> int main() { std::unordered_map<std::string, int> toto; toto["Outlook"] = 454; toto["Temperature"] = 4; toto["Humidity"] = 554; toto["Wind"] = 545454; std::unordered_map<std::string, int>::iterator It = toto.begin(); std::cout << toto.size() << std::endl; for (; It != toto.end(); ++It) std::cout << (*It).first <

std::unordered_map with custom value type, operator[]

自作多情 提交于 2020-01-05 08:06:46
问题 I'm trying to use std::unordered_map, as shown in the example here. class CSVRecord { public: CSVRecord(string csvLine) : _fields(vector<string>()) {...} vector<string> _fields; }; int main(int argc, char* argv[]) { unordered_map<string, CSVRecord> m; CSVRecord rec = CSVRecord("test"); m["t"] = rec; return 0; } However, m["t"] = rec gives an error: no matching function for call to ‘CSVRecord::CSVRecord()’ . I used m.insert(pair<string, CSVRecord>("t",rec)) instead, but I wonder why the