简体   繁体   English

在(C / C ++)中将一维数组分配为二维数组的元素

[英]Assign single dimension array as an element of two(multi) dimensional array in (C/C++)

Please apologize for my English first. 请先为我的英语道歉。 Let us typedef single and two(multi) dimensional arrays respectively as below: 让我们分别键入一维和二维数组,如下所示:

typedef float VERTREX[3];
typedef VERTREX TRIANGLE[3];

then say I have initialized some VERTEX arrays, 然后说我已经初始化了一些VERTEX数组,

VERTREX v1 = { 1, 2, 3 };
VERTREX v2 = { 2, 2, 3 };
VERTREX v3 = { 1, 2, 1 };

Assume mathematically a Triangle defined by combination of three vertices,therefore I defined a Triangle as following code snippet, 数学上假设由三个顶点组合定义的三角形,因此我将三角形定义为以下代码段,

TRIANGLE tr;

Problem arisen when I am going to assign each VERTEX (single dimension array) elements in to TRIANGLE (Array of arrays/2-Dimensional array) as below code, 当我要按以下代码将每个VERTEX (单维数组)元素分配给TRIANGLE (数组Array / 2维数组)时出现问题,

tr[0] = v1; // error C2106: '=' : left operand must be l-value(in Visual C++ compiler)
tr[1] = v2; //  error C2106:
tr[2] = v3; //  error C2106:

Also I cannot continue with creating array of Triangles too. 我也不能继续创建三角形数组。

TRIANGLE tr[4]; // creating array of Triangles

hence same behavior can be expected. 因此可以预期相同的行为。 If someone has an idea/solution how to assign Single Dimension array as an element of Two(Multi) Dimensional array please respond.Please do not provide solution with standard containers like std::vector or using raw pointers approach. 如果有人对如何将一维数组分配为二维多维数组的元素有想法/解决方案,请回答。请不要为标准容器(如std :: vector或使用原始指针方法)提供解决方案。 Please bound to array concept. 请绑定到数组概念。 Thank you everyone for listening. 谢谢大家的收听。 Please provide clean answer. 请提供干净的答案。

typedef s are aliases. typedef是别名。 They don't define new types. 他们没有定义新的类型。

TRIANGLE tr;
tr[0] = v1;

is similar to: 类似于:

VERTEX temp;
temp = v1;

If you remove typedef and use the actual types, that is equivalent to: 如果删除typedef并使用实际类型,则等效于:

float v1[3] = { 1, 2, 3 };
float temp[3];
temp = v1;

That is not allowed in C/C++. 这在C / C ++中是不允许的。 You cannot assign to an array like that. 您不能分配给这样的数组。

You'll need to copy the elements one by one, or use memcpy . 您需要一个一个地复制元素,或使用memcpy

for (int i = 0; i < 3; ++i )
   tr[0][i] = v1[i];       

or 要么

memcpy(tr[0], v1, sizeof(v1));

When you use an array of TRIANGLE s, you'll have to use a similar strategy to copy a TRIANGLE to an element of the array. 使用TRIANGLE数组时,必须使用类似的策略将TRIANGLE复制到数组的元素。


Since these sizes are defined at compile time, I strongly suggest use of std::array . 由于这些大小是在编译时定义的,因此我强烈建议使用std::array

Here's a simple program: 这是一个简单的程序:

#include <iostream>
#include <array>

using VERTEX = std::array<float, 3>;
using TRIANGLE = std::array<VERTEX, 3>;

int main()
{
   VERTEX v1 = { 1, 2, 3 };
   VERTEX v2 = { 2, 2, 3 };
   VERTEX v3 = { 1, 2, 1 };

   TRIANGLE tr;
   tr[0] = v1;

   TRIANGLE trArray[4];
   trArray[0] = tr;

   std::cout << v1[0] << " " << v1[1] << " " << v1[2] << std::endl;
}

and here's the output: 这是输出:

1 2 3 

You can do something like this if you really need to use typedefs: 如果确实需要使用typedef,则可以执行以下操作:

typedef float VERTREX[];
typedef float* TRIANGLE[3];

int main() {

  VERTREX v1 = { 1, 2, 3 };
  VERTREX v2 = { 2, 2, 3 };
  VERTREX v3 = { 1, 2, 1 };

  TRIANGLE tr;

  tr[0] = v1;
  tr[1] = v2;
  tr[2] = v3;

  TRIANGLE triangle[4];
  return 0;
}

But I would highly recommend using the std::array approach. 但我强烈建议您使用std :: array方法。 It is less prone to errors and misuse. 它不太容易出错和滥用。 Users won't know that VERTREX or TRIANGLE are arrays. 用户不会知道VERTREX或TRIANGLE是数组。

typedef float VERTREX[3];
typedef VERTREX* TRIANGLE[3];

int main()
{
    VERTREX v1 = { 1, 2, 3 };
    VERTREX v2 = { 11, 21, 13 };
    VERTREX v3 = { 1, 12, 41 };


    TRIANGLE tr;
    tr[0] = &v1;
    tr[1] = &v2;
    tr[2] = &v3;
    tr[3] = &v3;  --(1)// Concept(Array size) violation - Expect Runtime error 

    std::cout << (*(&v3))[2] << std::endl;
    std::cout << (*tr[2])[2] << std::endl;

    system("Pause");
    return 0;
}

// Compile with Visual C++(v.120) with Warning Level 4 ,No warning if we eliminate the line (1), here output: 41 41 //使用警告级别4的Visual C ++(v.120)进行编译,如果我们消除第(1)行,则没有警告,此处输出:41 41

All answers are correct but I will try simplify explanation of that error. 所有答案都是正确的,但我将尝试简化该错误的解释。 Even if it looks complicated it is quite simple: 即使看起来很复杂,它也很简单:

Whole error is caused just because you can't assign to array: 导致整个错误只是因为您无法分配给数组:

int array[5];
int another_array[5];
array = another_array; //error, can't assign to array

Now, let's try it with multidimensional array: 现在,让我们尝试使用多维数组:

int array_of_array[5][2];
int regular_array[2];
array_of_array[0] = regular_array; // error can't assign to array

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

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