将单维数组指定为两(多)维数组的元素 [英] Assign single dimension array as an element of two(multi) dimensional array

查看:85
本文介绍了将单维数组指定为两(多)维数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们分别输入单个和两个()维数组,如下所示:

Let us typedef single and two(multi) dimensional arrays respectively as below:

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



然后说我已经初始化了一些VERTEX数组,


then say I have initialized some VERTEX arrays,

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



以数学方式假设由三个顶点组合定义的三角形,因此我将Triangle定义为以下代码片段,


Assume mathematically a Triangle defined by combination of three vertices,therefore I defined a Triangle as following code snippet,

TRIANGLE tr;



当我要分配时出现问题每个VERTEX(单维数组)元素到TRIANGLE(数组数组/二维数组)如下代码,


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,

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



因此与预期相同的行为。

如果有人有想法/解决方案如何将单维数组指定为二(多)维数组的元素请回复。请做不提供标准容器的解决方案,如std :: vector或使用原始指针方法。

请绑定数组概念。

谢谢大家的聆听。





我尝试过:



尝试创建指向每个Vertex数组的指针,但是,它失败了我的优化概念。


hence same behavior as 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.
Please bound to array concept.
Thank you everyone for listening.


What I have tried:

Tried to create pointers to each Vertex arrays,but,it failed my optimization concept.

推荐答案

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;
}





//使用警告级别4编译Visual C ++(v.120),如果我们消除了第(1)行,这里输出:41 41



// Compile with Visual C++(v.120) with Warning Level 4 ,No warning if we eliminate the line (1), here output: 41 41


typedef float VERTREX[3];
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;
}


这篇关于将单维数组指定为两(多)维数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆