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.

Edited 4 Years Ago by badboy11: n/a

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.

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.

Edited 4 Years Ago by badboy11: n/a

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.

Edited 4 Years Ago by thines01: n/a

Thanks guys, I think I'll go for multiple vector<types> 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.

This question has already been answered. Start a new discussion instead.