stdmap

c++03: default constructor for build-in types in std::map

微笑、不失礼 提交于 2020-01-01 10:19:12
问题 I always thought that following code std::map<int, int> test; std::cout << test[0] << std::endl; would print random value, because it would create unitialized value within map. However, it turns out that created int is actually always initialized to zero AND standard builtin types are also zero-initialized in certain circumstances. The question is : when zero-initialziation is performed for standard types (int/char/float/double/size_t)? I'm pretty sure that if I declare int i; in the middle

no matching constructor for initialization of 'mapped_type' std::map error

為{幸葍}努か 提交于 2019-12-30 18:54:09
问题 I have this a class called 'Card' and I am trying to store some of its objects in a std::map Card.hpp: class Card { public: enum ValueType { NOVALUE, ACE }; enum FaceType { NOFACE, CLUBS }; Card(const ValueType & _value, const FaceType & _face); Card(const Card & _card); private: ValueType m_value; FaceType m_face; }; Here is how I store and access it: Deck.hpp: #include <map> class Card; class Deck { public: Deck(); std::size_t length() const; Card get_card(const int & _num); private: std:

How can I display the content of a map on the console?

限于喜欢 提交于 2019-12-30 00:15:36
问题 I have a map declared as follows: map < string , list < string > > mapex ; list< string > li; How can I display the items stored in the above map on the console? 回答1: Well it depends on how you want to display them, but you can always iterate them easily: typedef map<string, list<string>>::const_iterator MapIterator; for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++) { cout << "Key: " << iter->first << endl << "Values:" << endl; typedef list<string>::const_iterator

How can I display the content of a map on the console?

做~自己de王妃 提交于 2019-12-30 00:15:09
问题 I have a map declared as follows: map < string , list < string > > mapex ; list< string > li; How can I display the items stored in the above map on the console? 回答1: Well it depends on how you want to display them, but you can always iterate them easily: typedef map<string, list<string>>::const_iterator MapIterator; for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++) { cout << "Key: " << iter->first << endl << "Values:" << endl; typedef list<string>::const_iterator

C++ const std::map reference fails to compile

故事扮演 提交于 2019-12-29 05:50:12
问题 Is there a reason why passing a reference to a std::map as const causes the [] operator to break? I get this compiler error (gcc 4.2) when I use const: error: no match for ‘operator[]’ in ‘map[name]’ Here's the function prototype: void func(const char ch, std::string &str, const std::map<std::string, std::string> &map); And, I should mention that there is no problem when I remove the const keyword in front of std::map . If I've been instructed correctly, the [] operator will actually insert a

map insert: class as key and class as value

青春壹個敷衍的年華 提交于 2019-12-25 08:53:52
问题 I have two class StorageAddress and PersistentChunk. I define a map called SsdChunkMap. typedef std::map<StorageAddress, PersistentChunk> SsdChunkMap; SsdChunkMap* _SsdChunkMap; I want to insert data to map. StorageAddress _StorageAddress(victim._addr); PersistentChunk _PersistentChunk(victim._hdr.pos.offs, victim._data, victim._hdr.compressedSize); _SsdChunkMap->insert(make_pair(_StorageAddress, _PersistentChunk)); And I overload the operator < inline bool operator < (StorageAddress const&

Can I extend std::map::lower_bound to search on non-key_type arguments?

情到浓时终转凉″ 提交于 2019-12-24 19:29:57
问题 Here is an illustration of my situation. I have a std::map and I want to find the first pair<key,value> where the key is any member of an equivalence class of keys. #include <map> struct Category { int foo; int bar; bool operator < (const Category & rhs) const; bool operator > (const Category & rhs) const; }; struct Key { Category category; float quality; bool operator < (const Key & rhs) const { if (category < rhs.category) return true; else if (category > rhs.category) return false; else

Is it possible to handle std::ofstream with std::map?

耗尽温柔 提交于 2019-12-24 01:09:58
问题 "Handling map of files in c++" says no, one shall use std::map<std::string, std::ofstream*> , but this leads to the new and delete actions, which is not so neat. Since "Is std::ofstream movable? Yes!" and it's possible to "std::map<>::insert using non-copyable objects and uniform initialization", is it possible to handle a collection of ofstream using std::map ? so that one won't worry about closing filestreams and delete to release memory. I can compromise that during using std::map<std:

OpenMP Parallelizing for loop with map

拈花ヽ惹草 提交于 2019-12-24 00:41:41
问题 I am trying to parallelize a for-loop which scans std::map. Below is my toy program: #include <iostream> #include <cstdio> #include <map> #include <string> #include <cassert> #include <omp.h> #define NUM 100000 using namespace std; int main() { omp_set_num_threads(16); int realThreads = 0; string arr[] = {"0", "1", "2"}; std::map<int, string> myMap; for(int i=0; i<NUM; ++i) myMap[i] = arr[i % 3]; string is[NUM]; #pragma omp parallel for for(map<int, string>::iterator it = myMap.begin(); it !=

nested std::map using pointers

 ̄綄美尐妖づ 提交于 2019-12-23 23:16:50
问题 I am using a map inside a map and want to access a specific member in the second map. std::map<int, std::map<DWORD,IDLL::CClass*>*> MyMap 回答1: Try auto outerIt = MyMap.find(someInt); if(outerIt != MyMap.end()) { auto innerIt = (*outerIt)->find(someDWord); if(innerIt != (*outerIt)->end()) { auto yourElement = *innerIt; } } 回答2: If you are sure the keys exist, you could also try: IDLL::CClass* x = (*MyMap[key1])[key2]; 回答3: You can use std::map::find in two steps: first to find the value