C++ 多类型数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19678011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:02:49  来源:igfitidea点击:

C++ multiple type array

c++arrays

提问by darxsys

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:

是否可以在 C++ 中创建不同类型的多个有序元组的数组?例如,我希望能够创建一个元组数组,其中每个元组包含一个整数、一个字符串和一个双精度数?所以像:

  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?

有了这个,我可以有一个元组 (2,"3", 5.43)。这里的问题是,通常我事先不知道元组的大小。因此,它可能只有两个元素,或者五个元素,或者示例中的三个元素以及所有不同的类型。而且顺序也可能不同。是否可以在 C++ 中做这样的事情,否则我将不得不切换到 Python?

采纳答案by Ivaylo Strandjev

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

向量 inc++将具有相同类型的所有元素。另一种选择是你有 avectorvectors 但同样内部向量的元素必须是相同的类型。

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.

可能您尝试解决的问题比您尝试实现的问题有更好的解决方案。有一个丑陋且绝对不可取的解决方案 - 使用,vector<vector<void*> >但这既危险又无法维护。

If you will only have elements of a given set of types than create an abstract type that has an implementation for all there types. For instance define MyTypeand inherit it in MyTypeInt, MyTypeDoubleand MyTypeString. Than 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).

如果您只有一组给定类型的元素,则创建一个抽象类型,该类型具有所有类型的实现。例如MyTypeMyTypeInt,MyTypeDouble和 中定义和继承它MyTypeString。比声明一个vector<vector<MyType*> >实例(更好的是使用 scoped_array 或类似的东西而不是内部向量)。

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

编辑:根据 nijansen 评论,如果 boost 可用,您可以创建 Boost.Variant 向量的向量。

回答by Vishnu Kanwar

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.

数组是对象(相同大小)的系统排列。在 C/C++ 中,您不能创建可变大小元素的数组。

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;

回答by Lucif

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

所以我已经在研究这个名为 Nile 的只有头文件的项目。它在 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*

它还包含来自 push、pop 等的几个函数。目前它只支持 int、long、double、long double、float、std::string、const char*