954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Equivalent of C# Arraylist to hold Multiple Data Types

Looking for an equivalent container class that matches to C#'s Arraylist collections class. Is there anything that comes close to a container that can provide index based enumeration as well as hold multiple data types.

I am trying to create an vector that can hold multiple data types. For example:

//Is this line of code possible??
vector <int, string> vec;

int i=0;
string str = "test";

//add different data-type objs into my vector mutable array
vec.push_back(i);
vec.push_back(str);


Please advise, if there are other container classes in C++ that might help me achieve this functionality or there is a way to use vector class to store multiple data-types.

badboy11
Newbie Poster
18 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

I think a good question to ask is why do you think you need this behavior? Even in C# it's recommended to avoid ArrayList in favor of the more statically typed generic List<>. C++ isn't designed with a universal Object class that everything derives from, which makes a heterogeneous collection awkward on top of the usual risks.

But if you really need it, look into the Boost libraries. The Boost::any type would probably be what you want.

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 

Thank you for your response. I would on any other day agree on C# Generics but the design for this project is different. There are no universal Base class used here so I won't be able to implement an Abstract factory pattern. Another alternative I had in mind was using separate containers inside my class to track each data type.

//Internally manage the storage containers...
vector<type1> v; 
vector<type2> v; //etc.


I am trying to implement a Model-View-Controler. This post is related to implementing the Controller. Also the controller will be a Singleton.I think a good question to ask is why do you think you need this behavior? Even in C# it's recommended to avoid ArrayList in favor of the more statically typed generic List<>. C++ isn't designed with a universal Object class that everything derives from, which makes a heterogeneous collection awkward on top of the usual risks.

But if you really need it, look into the Boost libraries. The Boost::any type would probably be what you want.

badboy11
Newbie Poster
18 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

I agree with @deceptikon that it is not optimal to circumvent the "typing" in place for a language that uses it heavily.

[Alternatively...]
You can look into ArrayList in managed C++ (or C++/CLI) -- same animal, slightly different syntax.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Thanks guys, I think I'll go for multiple vector option. Although not ideal since as more concrete classes are added then I would need to change my Controller class code to match up, but it is a lot quicker.

I might also see if I could convince the other members to use the boost library, so we could use either 'variant' or 'any' classes.

I agree with @deceptikon that it is not optimal to circumvent the "typing" in place for a language that uses it heavily.

[Alternatively...] You can look into ArrayList in managed C++ (or C++/CLI) -- same animal, slightly different syntax.

badboy11
Newbie Poster
18 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: