10

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?

5
  • If you don't know the types ahead of time, how will you even write the code, nevermind the storage? Commented Oct 30, 2013 at 9:13
  • I know possible types, just not how many of them I will have in this tuple and in which order. Commented Oct 30, 2013 at 9:13
  • Do you need to care about size? Doesn't tuple allow any size you want? Commented Oct 30, 2013 at 9:13
  • You can use structures (struct) to declare your triplet. And you do a std::vector<triplet>; Commented Oct 30, 2013 at 9:14
  • 4
    @darxsys If that is determined only at runtime you'll probably have to resort to something like std::vector<std::vector<boost::variant<Types...>>>? Commented Oct 30, 2013 at 9:15

3 Answers 3

15

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;
Sign up to request clarification or add additional context in comments.

Comments

6

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.

1 Comment

Boost.Variant is certainly a better solution than void* if Boost libs are available
1

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*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.