Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ multiple type array

Tags:

c++

arrays

Is it possible to create an array of multiple ordered tuples of different types in C++? For example, I would like to be able to make an array of tuples of which every tuple contains one int, one string and one double? So something like:

  vector<pair<pair<int, string>, double> >;

With this I could have a tuple (2,"3", 5.43). The problem here is that in general I don't know the size of the tuple in advance. So, it could be only two elements, or five elements, or three as in the example and all different types. And the order could also be different. Is it possible to do something like this in C++ or I will have to switch to Python?

like image 552
darxsys Avatar asked Oct 30 '13 09:10

darxsys


People also ask

Can an array have multiple data types in C?

No, we cannot store multiple datatype in an Array, we can store similar datatype only in an Array.

Can an array contain different data types C++?

"Arrays can only contain objects of one type." What about using arrays of union or std::variant<>? @GabrielStaples Those arrays contain objects of the union type, or objects of the variant type.

What are the types of array in C?

There are two types of array: Two-dimensional array. Multi-dimensional array.

Can a vector store different data types?

The first collection type we'll look at is Vec<T> , also known as a vector. Vectors allow you to store more than one value in a single data structure that puts all the values next to each other in memory. Vectors can only store values of the same type.


2 Answers

An array is a systematic arrangement of objects (of the same size). In C/C++ you can't create an array of variable size elements.

However, you can use polymorphism to active this.

Create an array of abstract type pointer and cast an array element based on its type.

Example:

namespace Array {
    enum Type  {
        Type1T,
        Type2T,
    };

    class AbstractType {
        public:
            virtual Type GetType() = 0;
            virtual ~AbstractType() {} 
    };

    class Type1 : public AbstractType  {
        public:
            Type GetType() { return Type1T;}

            int a;
            string b;
            double c;
    }; 

    class Type2 : public AbstractType  {
        public:
            Type GetType() { return Type2T;}

            int a;
            string b;
            string c;
            double d; // whatever you want
    };
}

And create your array of multiple different types as;

vector<Array::AbstractType*>  my_array;
like image 126
Vishnu Kanwar Avatar answered Sep 22 '22 22:09

Vishnu Kanwar


A vector in c++ will have all of its elements with the same type. An alternative is to have a vector of vectors, but again, the elements of the inner vectors will have to be of the same type.

Probably the problem you try to solve will have a better solution than what you try to achieve. There is an ugly and definitely not advisable solution - to use vector<vector<void*> > but this is both dangerous and unmaintainable.

If you will only have elements of a given set of types, then create an abstract type that has an implementation for all there types. For instance, define MyType and inherit it in MyTypeInt, MyTypeDouble and MyTypeString. Then declare a vector<vector<MyType*> >, for instance, (even better would be to use a scoped_array or something of the sort instead of the inner vector).

EDIT: as per nijansen comment, if boost is available you can create a vector of vectors of Boost.Variant.

like image 26
Ivaylo Strandjev Avatar answered Sep 22 '22 22:09

Ivaylo Strandjev