简体   繁体   English

数组中有多个变量类型

[英]More than one variable type in array

I am busy with a dynamic 2d array and I have declared it as follows: 我正在处理动态2D数组,并声明如下:

string** MakeArray(int row,int col)
{
    string** array;
    array = new string* [col];

    for(int r = 0; r < row; r++)
    {
        array[r] = new string [col];
    }
    return array;
}

Now I can place string values in the array. 现在,我可以将字符串值放在数组中。 How can I place Integer values in the first column and strings in second and integers in third, if my array is 4 by 99? 如果我的数组是4 x 99,如何将Integer值放在第一列,将字符串放在第二列,将整数放在第三列?

The elements in an array are all the same type. 数组中的元素都是相同的类型。 To get what you're after, you probably want to start off rather differently, with an array of structs: 为了获得您想要的东西,您可能想以不同的方式开始,使用一系列结构:

struct whatever { 
    int a;
    std::string b;
    int c;
};

std::vector<whatever> your_array;

Edit: although it's almost certainly a lousy idea, if you really need this to be a 2D array, you could try making all your elements the same type of union: 编辑:尽管这几乎是一个糟糕的主意,但是如果您确实需要将其作为2D数组,则可以尝试使所有元素成为同一类型的并集:

union whatever { 
    int a;
    std::string b;
};

This has some pretty severe limitations though -- in fact, putting a std::string in a union isn't officially supported at all. 但是,这有一些相当严格的限制-实际上,正式不支持将std::string放入联合体。 There's a fairly decent chance it'll work, but no guarantee of it at all. 它很有可能会运行,但完全不能保证。 If you really, really need to do something like this, you can make that member of the union a pointer instead. 如果确实需要执行类似操作,则可以使该联合成员成为指针。 That is guaranteed to work, but also guaranteed to be so clumsy that making mistakes with it is nearly inevitable. 这样可以保证工作,但也很笨拙,几乎不可避免地会犯错误。

Don't do that. 不要那样做 Instead create a struct that will represent single record in a table, and contain a string and two integers. 而是创建一个结构,该结构将表示表中的单个记录,并包含一个字符串和两个整数。 Then create one dimensional array of those structures. 然后创建这些结构的一维数组。

struct record
{
    int a;
    std::string b;
    int c;
};

record* MakeArray(int row)
{
    return new record[row];
}

better yet, ditch arrays and use std::vector: 更好的是,沟数组并使用std :: vector:

std::vector<record> array(99); 

Have you looked at having a vector/array of tuples, if you have C++11 available to you? 如果可以使用C ++ 11,您是否考虑过使用元组向量/数组? So you could do something such as: 因此,您可以执行以下操作:

#include <tuple>
#include <vector>

typedef std::tuple<int, std::string, int> MyTuple;

std::vector<MyTuple> MakeVector()
{
    std::vector<MyTuple> vecTuples;

     for( int i = 0; i < 5; ++i )
     {
         vecTuples.emplace_back( std::make_tuple<int, std::string, int>( i, "Test"+i, i+5 ) );
     }

     return vecTuples;
}

C++ is a "strong-typed" language. C ++是一种“强类型”语言。 One of the things that means is you cannot mix data types (unless they are related, like base-derived class hierarchical relationship). 意思之一就是您不能混合数据类型(除非它们是相关的,例如基类继承关系)。

In other words what you are doing is not what C++ directly supports. 换句话说,您所做的不是C ++直接支持的。

Having said that there's something you can do that would do what you want - have an array of triplets , like this: 话虽这么说,您可以做一些您想做的事-拥有三胞胎数组,如下所示:

struct triplet
{
  int first;
  string second;
  int third;
};

triplet** MakeArray(...

What you are doing in your example looks alot like a JS code though. 但是,您在示例中所做的事情看起来很像JS代码。 Maybe what you want is to store all your data as strings? 也许您想要将所有数据存储为字符串? Then yes, you can use a 2D array of strings, but that requires you to convert datum into string when storing it and converting back from string for calculations. 然后是的,您可以使用2D字符串数组,但这需要您在存储数据时将基准转换为字符串,并从字符串转换回进行计算。 Which is a major performance issue 这是一个主要的性能问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM