1

Related to a different question I have posted for which I am trying to find a solution some how - C++ dynamic way to determine the container to use

Noticed similar links:

How to use a string as a variable name in C++?

How to adress variable by name stored in another variable (C++)

Edited - From int to float..

Edit 2 - the following seems to have some solution for this at this link - but need to somehow translate for this problem at hand..

How to create instance of a class when the class name is stored in another variable

I have the following structure:

Class Items
{
  float itemType;
  float itemPrice;
  Items(){};
  ~Items(){};
};

typedef std::unique_ptr<Items> pptr;
typedef std::pair<float, pptr> itemsRec;
std::multimap<float, pptr> mapItems;

Need help with creating a new map of mapItems... can any one help with the syntax to create this new map of mapItems? Let's assume "A" gets derived by other means.

Thanks

std::map<string, pointer to mapItems> myMap;  // What's the syntax for this?

myMap["A"]={create a new map - mapItems_A and assign pointer here};

Thanks.

Edit 3 - Still exploring options - Looks like the following link has some interesting stuff that might be helpful in my case to define the container dynamically.. Exploring further.

http://sourcemaking.com/design_patterns/factory_method/cpp/1#

Community
  • 1
  • 1
ejuser
  • 181
  • 2
  • 5
  • 13

2 Answers2

2

The syntax for the type you requested would be std::map<std::string, std::multimap<float, pptr>*>, but you don't need to use a pointer; you could just use std::map<std::string, std::multimap<float, pptr> >

std::map::operator[] automatically default-constructs an entry and inserts it when the key requested is not present so if the mapped type is std::multimap<float, pptr>, it will automatically construct the multimap corresponding to a specific key in the map when it is needed. For example:

    std::map<std::string, std::multimap<float, pptr> > myMap;
    std::multimap<float, pptr>& myMap_A = myMap["A"];

If the mapped type is std::multimap<float, pptr>* then it's just a raw pointer, so it would be initialized to nullptr (0), and you would need to manually detect that, allocate a new multimap for that key, and store its address. You would also need to make sure that you iterate through the map and delete all the dynamically allocated multimaps before the map is destroyed, and make sure that you never overwrite a non-null pointer without deleteing its multimap first. All of this is can be avoided by not using raw pointers as the mapped type. In fact, you should in general avoid using raw pointers that have ownership of their data; use smart pointers instead.

One last note: Using floating point types as container keys is somewhat risky, since it's quite easy to encounter values that are not quite equal due to rounding errors.

Community
  • 1
  • 1
bcrist
  • 1,520
  • 13
  • 25
  • Thanks bcrist. Wondering if it is possible to dynamically generate the variable name myMap_A - from input variable "A" by some means which in turn can be used above ( might not be technically possible - but thought of asking ). – ejuser May 17 '13 at 03:22
  • I think you might be somewhat confused about variables. The only way to create a variable name is put it into a source or header file. The compiler then decides where that variable's data will be stored. You can't "generate" new variable names at runtime. When you want to refer to data located somewhere else, you need to use references or pointers, as in `std::multimap& myMap_A = myMap["A"];` – bcrist May 18 '13 at 05:59
1
std::map<std::string, std::multimap<float, pptr>*> myMap;
std::multimap<float, pptr>* mapItems_A = new std::multimap<float, pptr>();
myMap["A"] = mapItems_A;

Keep in mind that your map points to an empty multimap.

lekroif
  • 992
  • 3
  • 12
  • 24
  • Thanks lekriof. Appreciate it. Going to give it a try.. Any suggestion on making mapItems_A - dynamically from input parameter "A"? Thanks ( most likely this is not possible.. but wondering if there are new tricks to do with c++11 or so ).. thanks – ejuser May 17 '13 at 02:33
  • I am not sure I understand what you mean, but if you are trying to just shrink it to one line (so you don't use and intermediate pointer to store the reference to the multimap): myMap["A"] = new std::multimap(); – lekroif May 17 '13 at 03:38