C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-03-2009, 06:33 PM   #1
Registered User
 
Join Date: Sep 2009
Posts: 7
C++ Map with multiple data types?

Hi everyone!

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   Reply With Quote
Old 10-03-2009, 07:12 PM   #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   Reply With Quote
Old 10-03-2009, 08:28 PM   #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   Reply With Quote
Reply

Tags
c++, container, data, map, type

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22