简体   繁体   English

多维字符数组

[英]Multidimensional Character Array

您如何在C ++中初始化和取消初始化多维字符数组?

Read the FAQ -- you'll find everything you need there! 阅读常见问题解答 -您将在那里找到所需的一切!

Creation: 创建:

Statically allocated: 静态分配:

char mda[ dim1_size ][ dim2_size ]...[ dimn_size ];

Dynamically allocated: Nested new[] calls. 动态分配:嵌套的new[]调用。

Initialization: 初始化:

Nested for loops; 嵌套循环; as many for as your dimensions. 尽可能多for为你的尺寸。

Unitialization: Do you mean Destruction? 统一化:您是说破坏?

Statically allocated: The compiler does this when the stack frame is unwound (or for global variables -- when the program stops running). 静态分配:展开堆栈帧时(或对于全局变量-程序停止运行时),编译器将执行此操作。

Dynamically allocated: 动态分配:

Using nested delete[].

I suggest you use the Boost.Multi_Array library. 我建议您使用Boost.Multi_Array库。 The dimension of the array has to be provided at compile-time, but the sizes are only used at runtime. 数组的维数必须在编译时提供,但是大小只能在运行时使用。 That means that you have the advantages of dynamic allocation without the pain of having to deal with memory issues. 这意味着您具有动态分配的优点,而不必处理内存问题。

Here goes the example from the Boost documentation . 这里是Boost文档中的示例。

int 
main () {
  // Create a 3D array that is 3 x 4 x 2
  typedef boost::multi_array<double, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  // Assign values to the elements
  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;

  // Verify values
  int verify = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        assert(A[i][j][k] == verify++);

  return 0;
}

A quick snippet - it compiles in g++. 一个简短的代码片段-它以g ++编译。

int rows = 10;
int cols = 10;

char** array = new char*[rows];
for( int i = 0; i < cols; ++i ) {
  array[i] = new char[cols];
}

//do stuff with array

for( int i = 0; i < cols; ++i ) {
  delete array[i];
}
delete array;

This is interesting, and this requires a serious look. 这很有趣, 并且需要认真看。

The answer given by roo is a widely used one, but I like his observation - just because it compiles doesn't mean it works Roo给出的答案是一种广泛使用的答案,但是我喜欢他的观察- 只是因为它可以编译并不意味着它有效

I would think a better solution would be to allocate a contigious block of memory (rows * cols) long and then treat it as a 2D array? 我认为更好的解决方案是分配一个连续的内存块(行*列),然后将其视为2D数组?

you can initialize a multidimensional array like this: 您可以像这样初始化多维数组:

int grid[2][3] = {1, 3, 5, 2, 4, 6};

in that case the seperate values would be: 在这种情况下,单独的值将是:

grid[0, 0]: 1
grid[0, 1]: 3
grid[0, 2]: 5
grid[1, 0]: 2
grid[1, 1]: 4
grid[1, 2]: 6

I learned it with a tic tac toe board 我是用井字游戏板学习的

const int ROW = 3;
const int COLUMN = 3;
char board [ROW] [COLUMN] = {{'O', 'X', 'O'},
                             {'X', 'X', 'X'},
                             {'X', 'O', 'X'}};

I hope this helped. 希望对您有所帮助。

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

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