stdmap

Accessing a std::map of std::map like an array

醉酒当歌 提交于 2021-02-11 16:55:08
问题 I have initialize a std::map of a std::map like as below: static std::map<std::string, std::map<std::string, float>> _ScalingMapFrequency = { {"mHz", {{"mHz", 1.0}}}, {"mHz", {{"Hz", 1e-3}}}, {"Hz", {{"mHz", 1e+3}}}, {"Hz", {{"Hz", 1.0}}}}; And now I am trying to access the float values in the following way: std::cout<<" the scaling factor is :"<<_ScalingMapFrequency["mHz"]["Hz"]; There is no problem when I compile and run the code but I am expecting to get "1e-3" instead I am always getting

Why doesn't a call to std::map::operator[] compile for a value type without default constructor? [duplicate]

时光怂恿深爱的人放手 提交于 2021-02-05 12:30:45
问题 This question already has answers here : Default constructor of the structure for correct std::map behaviour (2 answers) Closed 6 months ago . Consider the following class MyStruct : struct MyStruct { int x; int y; MyStruct(int i, int j): x(i), y(j) { } }; Note that MyStruct doesn't have a default destructor. The assignment m["AAAA"] = MyStruct(1, 1) in the code below doesn't compile: int main(int, char**) { map<string, MyStruct> m; m["AAAA"] = MyStruct(1, 1); return 0; } Why I need default

std::unique_ptr with std::map

谁都会走 提交于 2021-01-28 07:27:25
问题 I have a std::map where the key is std::shared_ptr<Foo> and the value is std::unique_ptr<Bar> where Foo and Bar are very different classes from a third-party library. I am using this std::map object as an in-memory cache. I am wondering what the best way of inserting a new entry into this map will be and then returned from a method, given that the Bar passed into the std::unique_ptr will already be constructed? I currently have the following: class SomeClass { public: const Bar*

Could not convert from '<brace-enclosed initializer list> to

我的未来我决定 提交于 2021-01-28 07:09:04
问题 I know that has a lot of questions similar, but I saw them and none of them helped me, I think is that because mine is kind of different, and at the same time weird. I made another question and a member answered to me, but instead of using classes he used structs. and it's working perfectly, but when I try to put it as classes, using the same logic, this is what happen, the error: error: could not convert '{{"foo", "bar"}}' from '' to 'B' I tried, but I don't know what is happening. #include

incomplete type as member of std::map

流过昼夜 提交于 2021-01-28 05:06:31
问题 I came about the same issue as described here Can't allocate class with forward declared value in std::map member variable in our codebase. Hoever I also found other cases where our compiler (MSVC 2017) is able to compile this... After fiddling around with the code I found that defining the con- & destructor in the cpp allows the files to compile. In test.h : #ifndef TEST_H #define TEST_H #include <map> struct Incomplete; class Test { std::map<int, Incomplete> member; public: Test(); ~Test();

I tried to add a new template helper function to my class and now I get a LNK2001 error. How to fix?

不羁岁月 提交于 2021-01-07 02:52:47
问题 I asked a couple of questions recently on StackOverflow to see if I could consolidate some functions into one by making use of templates. Those questions were: Can these methods that convert safe arrays into std::list objects be turned into a template function? Can this template function be adapted to account for the following method? I had one more function to try and update so I thought I would give it a go myself. This was the function to update: void CMSATools::ConvertSAFEARRAY_DATE

I tried to add a new template helper function to my class and now I get a LNK2001 error. How to fix?

旧街凉风 提交于 2021-01-07 02:52:36
问题 I asked a couple of questions recently on StackOverflow to see if I could consolidate some functions into one by making use of templates. Those questions were: Can these methods that convert safe arrays into std::list objects be turned into a template function? Can this template function be adapted to account for the following method? I had one more function to try and update so I thought I would give it a go myself. This was the function to update: void CMSATools::ConvertSAFEARRAY_DATE

std::map with different data types for values

自闭症网瘾萝莉.ら 提交于 2020-12-31 09:29:44
问题 I have been searching the forums and google and having a hard time understanding how I can do what I want. My example is based of typical dataset you see for an election. I want to split a delimited string and create a map to access later The string looks like this: "name=candidate1;vote=1000;percent=10.5" I am able to create my map of strings as follows while (getline(oss, key, '=') && getline(oss, value)) { mCanData.insert(std::pair<std::string, std::string>(key, value)); } What I would

std::map with different data types for values

烂漫一生 提交于 2020-12-31 09:24:04
问题 I have been searching the forums and google and having a hard time understanding how I can do what I want. My example is based of typical dataset you see for an election. I want to split a delimited string and create a map to access later The string looks like this: "name=candidate1;vote=1000;percent=10.5" I am able to create my map of strings as follows while (getline(oss, key, '=') && getline(oss, value)) { mCanData.insert(std::pair<std::string, std::string>(key, value)); } What I would