简体   繁体   English

我需要一个在 C++ 中具有一个动态维度的二维数组

[英]I need a 2-dimensional array with one dynamic dimension in C++

I need to create a 2-dimensions array, where there are 8 "rows" but "columns" are not predetermined我需要创建一个二维数组,其中有 8 个“行”但“列”不是预先确定的

someting along this example:一些沿着这个例子:

[0], ['mum', 'dad', 'uncle']
[1], ['brother', 'sister']
[2], ['friend', 'colleague', 'boss', 'employee']
.... and so on

than I have to scroll the first index (1 to 8) and read every possible values of all columns (for each "index" I have the count of how many elements are in it.比我必须滚动第一个索引(1 到 8)并读取所有列的每个可能值(对于每个“索引”,我都有其中包含多少元素的计数。

and I need to read using something like我需要使用类似的东西阅读

ary[2][3]    --->that would return "boss"

Following other exampes I am using按照我正在使用的其他示例

unsigned char (*ary)[n] = malloc(sizeof(unsigned char[8][n]));

but it doesn't compile and gives:但它不会编译并给出:

error: cannot initialize a variable of type 'unsigned char **' with an rvalue of type 'void *'错误:无法使用“void *”类型的右值初始化“unsigned char **”类型的变量

HOw do I declare and read this kind of array correctly in C++ ?我如何在 C++ 中正确声明和读取这种数组?

If the number of rows will be known at compile time you can use std::array 如果在编译时知道行数,则可以使用std::array

std::array<std::vector<std::string>, number_of_rows>

You can also use 您也可以使用

std::vector<std::string>[number_of_rows]

But working with raw arrays is not as convenient as std::array . 但是使用原始数组不如std::array方便。

If the number of rows will not be known until runtime you can have a vector of vectors like 如果行数直到运行时才知道,则可以有一个向量向量,例如

std::vector<std::vector<std::string>>

When using C++, avoid using malloc . 使用C ++时,请避免使用malloc Prefer to use new instead. 最好改用new

Assuming n is a compile time constant, you can use: 假设n是一个编译时间常数,则可以使用:

unsigned char (*ary)[n] = new unsigned char[8][n];

If n is a run time variable, you most likely need to use: 如果n是运行时变量,则很可能需要使用:

unsigned char (*ary)[8] = new unsigned char[n][8];

You can avoid the problems of dealing with dynamically allocated memory if: 在以下情况下,您可以避免处理动态分配的内存的问题:

  1. You use a std::array for the array of unsigned char . 您将std::array用于unsigned char数组。
  2. You use a std::vector to capture the dynamic nature of the data. 您可以使用std::vector捕获数据的动态性质。

std::struct<std::array<unsigned char, 8>> ary(n);

First, this syntax is more C than C++ (familiarize yourself with new for example, or arrays/vectors/STL etc...). 首先,此语法比C ++更具C语言(例如,熟悉new的知识,或者熟悉数组/向量/ STL等)。 In your case, if you really want C syntax (malloc) then only malloc when you know how much you want to malloc during run time. 在您的情况下,如果您确实想要C语法(malloc),则只有在知道在运行时要分配多少malloc时,才使用malloc。 You said you don't want the same n for all rows, so your line doesn't make logical sense (in addition to being illegal): 您说过,您不希望所有行都使用相同的n ,因此您的行没有逻辑意义(除了是非法的):

char *a[8];
a[0] = malloc(sizeof(char)*the_amount_of_chars_plus_null_termination_in_all_strings);
/* You have to test malloc succeeded! */
/*For each row a different size, which must be known at run time*/

You don't say how you obtain each row, so I don't know if you plan on per-calculating these values, or getting strings from input and then you can use strlen+1 for each one. 您没有说如何获取每一行,所以我不知道您是打算对这些值进行逐个计算,还是从输入中获取字符串,然后就可以对每个行使用strlen+1了。

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

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