C++ c+中的模板数组+;有3种不同的定义

C++ c+中的模板数组+;有3种不同的定义,c++,C++,我想创建一个具有3种不同定义的数组。更具体地说,我想要这样一个数组:A[i,j]=(string,int,bool 字符串,int,bool) 我试图创建一个模板,但我感到困惑。您可以创建一个实现此行为的常规类。 大概是这样的: class specialArray { char *** strings; int **integers; bool **booleans; public: specialArray(int rows, int colu

我想创建一个具有3种不同定义的数组。更具体地说,我想要这样一个数组:A[i,j]=(string,int,bool 字符串,int,bool)
我试图创建一个模板,但我感到困惑。

您可以创建一个实现此行为的常规类。 大概是这样的:

class specialArray {
    char *** strings;
    int **integers;
    bool **booleans;
    public:
        specialArray(int rows, int columns, unsigned char data[]) {
            //initialize arrays to correct sizes
            /*add a special values, such as 253, 254, 255 (which are not 
            used for chars, bools, or strings (since the operating system 
            takes control of memory locations 253, 254, 255)) every time the 
            type of the array changes*/
            /*Use the special values to determine which array each byte of 
            information should go to (and use casts). Arrays which are not 
            filled at each index should be set to 0 at that position*/
            /*create a function access(int loc) to obtain a value. In this 
            function, check each of the three arrays until one of the arrays 
            at that index value is not 0. Return a byte array.*/
            /*Implementing this in c++ is extremely inefficient, by the 
            way as you can see by the enormous amount of code this 
            requires*/
        }
        //do something similar for operator=()
}

如果要使用模板,可以创建结构数组:

template <class A, class B, class C>
struct Vars{
    A a;
    B b;
    C c;
};

//declaring the types of the template you want to use
Vars <std::string, int, bool> *ray;

//initializing array in heap
ray = new Vars<std::string, int, bool>[SIZE];

如果我理解正确的话,数组-AFAIK-只能有一个数据类型。但是,您可以使用struct/classes对两种不同的类型进行配对,然后从中生成一维数组

检查此代码插图,并检查它是否是您正在寻找的

template<typename T, typename U>
struct MyArray
{
    T   a;
    U   b;
};

int main()
{
    MyArray<int,bool> arr[2];

    arr[0] = {30,1};

    cout << arr[0].a;
    cout << " : ";
    cout << arr[0].b;
} 
模板
结构MyArray
{
Tα;
U b;
};
int main()
{
myarr阵列[2];
arr[0]={30,1};

C++可以不支持C++的语法吗?(C++,11),如果你有C++的访问权限,可以看看。否则你的具体要求不清楚。“我很困惑。”是的,我也是。我想要一个数组,我将填充o产品的数据。用户会给我一个描述,价格,以及产品是否是新的(真/假)我想把它放在一个由未知行和3列组成的数组中。我不确定我是否理解你所说的253、254、255个地址的意思。这些数字用于区分应该被读取为布尔、整数或字符串的数据。
template<typename T, typename U>
struct MyArray
{
    T   a;
    U   b;
};

int main()
{
    MyArray<int,bool> arr[2];

    arr[0] = {30,1};

    cout << arr[0].a;
    cout << " : ";
    cout << arr[0].b;
}