stdmap

Can't allocate class with forward declared value in std::map member variable

£可爱£侵袭症+ 提交于 2019-12-13 14:06:49
问题 In test.h : #ifndef TEST_H #define TEST_H #include <map> struct Incomplete; class Test { std::map<int, Incomplete> member; public: Test(); int foo() { return 0; } }; #endif In test.cpp : #include "test.h" struct Incomplete {}; Test::Test() {} In main.cpp #include "test.h" int main() { Test test; return test.foo(); } g++ 4.7 gives me an error that struct Incomplete is forward declared when I write g++ main.cpp test.h -o main.o . However, if I change std::map<int, Incomplete> member to std::map

[] operator in std::map is giving me segmentation fault [closed]

99封情书 提交于 2019-12-13 11:19:52
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I have a std::map<std::string, myClass*> myMap then I am inserting like follow: if(!myKey.empty()) { myMap[myKey] = this; } This sometime is throwing a segmentation fault. Why?? 回答1: Maybe your myMap is no longer

Storing std::map iteration object as itemdata in CGridCtrl

江枫思渺然 提交于 2019-12-13 06:36:30
问题 I have been trying to decide how to convey my problem. I have some maps: typedef struct tagDemoEntryAssign { COleDateTime datMeeting; CString strAssignment; int iAssignmentType; // AJT v16.0.9 int iStudyPoint; // AJT v16.0.3 int iNextStudyPoint; // AJT v16.0.9 } S_DEMO_ENTRY_ASSIGN; typedef std::vector<S_DEMO_ENTRY_ASSIGN> PublisherAssignments; typedef struct tagDemoEntryEx { CString strName; E_GENDER eGender; E_POSITION ePosition; E_APPOINTED eAppointed; BOOL bDemonstrations; // AJT v16.0.3

std::map - Element access without exception and without insertion

a 夏天 提交于 2019-12-13 01:26:38
问题 I have a recurrent pattern with the use of std::map. I want to retrieve the value only when the key is present, otherwise I don't want to insert element. Currently I'm using count(key) or find(key) (which one is better? from the documentation the complexity seems to be the same) and if them returns a positive value that I access the map. However I would like to avoid the use of two operations on the map. Something like: map<string, int> myMap; int returnvalue; boole result = myMap.get("key1"

Is there a transparent way of using unique_ptr in std containers?

半城伤御伤魂 提交于 2019-12-12 20:38:03
问题 Is there a transparent way of using std::unique_ptr in containers? #include <iostream> #include <memory> #include <map> struct method { virtual ~method() { std::cout << "f\n"; }; }; typedef std::unique_ptr<method> MPTR; std::map<int, MPTR> tbl; void insert(int id, method *m) { tbl.insert({id,std::unique_ptr<method>(m)}); }; void set(int id, method *m) { tbl[id] = std::unique_ptr<method>(m); }; int main(int argc, char **argv) { insert(1,new method()); set(1,new method()); return 0; } I'd like

Custom Key for std::map<> [duplicate]

拜拜、爱过 提交于 2019-12-12 18:14:05
问题 This question already has answers here : C++ const map element access (4 answers) Closed 4 years ago . I am trying to use the following struct as a custom key for std::map : struct ActionId { // ENCAPSULATED MEMBERS private: size_t _id; static size_t _root; static size_t incrementedRoot() { return (_root += 1); } // INTERFACE public: ActionId() : _id(incrementedRoot()) { } ActionId(const ActionId& that) : _id(that._id) { } ActionId& operator=(const ActionId& that) { this->_id = that._id;

Multimap containing pairs?

不想你离开。 提交于 2019-12-12 16:14:36
问题 Is it possible for a multimap to contain within it pairs? IE, rather then being defined as multimap<char,int> for instance, it would be defined as multimap<pair, pair >? How would this multimap then be sorted? Also, how would one access the individual contents of each pair? 回答1: Is it possible for a multimap to contain within it pairs? Yes its possible. How would this multimap then be sorted? By the key/first pair (ie, first by the first element of the first pair, then by the second element

How to use a boost::mutex as the mapped type in std::map?

会有一股神秘感。 提交于 2019-12-12 11:13:12
问题 I would like to lock the keys/index in another map like this: std::map<int, boost::mutex> pointCloudsMutexes_; pointCloudsMutexes_[index].lock(); However, I am getting the following error: /usr/include/c++/4.8/bits/stl_pair.h:113: error: no matching function for call to 'boost::mutex::mutex(const boost::mutex&)' : first(__a), second(__b) { } ^ It seems to work with std::vector , but not with std::map . What am I doing wrong? 回答1: In C++ before C++11, the mapped type of a std::map must be both

Is the value of primitive types in std::map initialized?

醉酒当歌 提交于 2019-12-12 10:31:56
问题 Consider the following code: map<int,int> m; for(int i=0;i<10000;++i) m[i]++; for(int i=0;i<10000;++i) printf("%d",m[i]); I thought the the values printed out would be undefined because primitive types doesn't have default constructor, but here I got 10000 1s every time I tested. Why is it initialized? 回答1: When invoking operator[] and the key is missing, the value is initialized using the expression mapped_type() which is the default constructor for class types, and zero initialization for

why the polymorphic types error and cleanup question?

喜欢而已 提交于 2019-12-12 10:21:17
问题 #include <iostream> #include <string> #include <map> #include <vector> class base {}; class derived1 : public base { public: unsigned short n; derived1() { n = 2; } }; class derived2 : public base {}; void main() { // way 1 { std::vector<derived1> a1; std::vector<derived2> a2; std::map<std::string, base*> b; a1.push_back(derived1()); b["abc"] = &a1.at(0); std::cout<<(dynamic_cast<derived1*>(b.find("abc")->second))->n<<std::endl; } // way 2 { std::map<std::string, base*> b; b["abc"] = new