![]() |
| | #1 |
| Registered User Join Date: Sep 2009
Posts: 7
| C++ Map with multiple data types? I'm doing a C++ problem that I'm a bit stuck on. I'd like to be able to store multiple data types in a C++ container with a key (such as a Map)... Is there any way to do this or another way I should be approaching this? Thanks |
| LuckyPierre is offline | |
| | #2 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 848
| since you can only have one "type", all entries in the map must be of that type. for example, a map with type "string" can only store objects of type "string" (of course). the only reason you would want to have the map store "different" types is if they are all related somehow, that is: they all extend some class. maybe you have a map to store objects of type "Vehicle". if you have child classes "Sedan", "Truck", etc, then those can be stored in the same map--because they are related. you wouldnt want to add an "Animal" to your "Vehicle" map because they arent related (of course unless you made them!). if you are trying to store different unrelated types (i.e. no parent-child relationship), then its probably a design issue (or this data structure doesnt make sense to use). hope it helps. |
| nadroj is offline | |
| | #3 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,122
| I agree that putting mixed unrelated types in a container seems a bit strange, but if you really want to, I think using the Boost Any class should work.
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
![]() |
| Tags |
| c++, container, data, map, type |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Generalising methods to handle multiple weapon types | Swarvy | Game Programming | 2 | 05-22-2009 02:52 AM |
| Reading a file with Courier New characters | Noam | C Programming | 3 | 07-07-2006 09:29 AM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| Warnings, warnings, warnings? | spentdome | C Programming | 25 | 05-27-2002 06:49 PM |
| Data types in Unicode | Garfield | Windows Programming | 12 | 10-28-2001 10:48 AM |