darxsys
darxsys

Reputation: 1570

C++ multiple type array

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?

Upvotes: 10

Views: 44505

Answers (3)

Vishnu Kanwar
Vishnu Kanwar

Reputation: 781

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 achieve 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 then create your array of multiple different types as:

vector<Array::AbstractType*>  my_array;

Upvotes: 15

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

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.

Upvotes: 6

Lucif
Lucif

Reputation: 119

So I was already working on this header only project called Nile. Which does the specific task in C++. https://github.com/LUCIF680/Nile

#include"nile.h"
Array y = {50,70.2,"ram"};

It also contains several function from push,pop etc. Currently it only supports int,long,double,long double,float,std::string,const char*

Upvotes: 1

Related Questions