在(C/ c++)中,将单维数组赋值为二维数组的元素

时间:2021-07-27 01:30:28

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,

然后假设我初始化了一些顶点数组,

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,

当我将每个顶点(单维数组)元素分配给三角形(数组数组/二维数组)时出现的问题如下代码所示,

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. Please bound to array concept. Thank you everyone for listening. Please provide clean answer.

因此,同样的行为是可以预期的。如果有人有一个想法/解决方案,如何分配单维数组作为一个元素的二维数组,请回应。请不要提供标准容器的解决方案,如std: vector或使用原始指针方法。请绑定到数组概念。谢谢大家的聆听。请提供清洁的答案。

4 个解决方案

#1


1  

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. It is less prone to errors and misuse. Users won't know that VERTREX or TRIANGLE are arrays.

但是我强烈推荐使用std::array方法。它不太容易出错和误用。用户不会知道VERTREX或TRIANGLE是数组。

#2


3  

typedefs are aliases. 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++. You cannot assign to an array like that.

这在C/ c++中是不允许的。不能像这样给数组赋值。

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 TRIANGLEs, you'll have to use a similar strategy to copy a TRIANGLE to an element of the array.

当您使用一个三角形数组时,您必须使用类似的策略将一个三角形复制到数组的一个元素中。


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 

#3


1  

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

//用visualc++ (v.120)进行编译,警告级别为4,如果删除行(1),则没有警告,这里输出:41 41。

#4


0  

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

#1


1  

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. It is less prone to errors and misuse. Users won't know that VERTREX or TRIANGLE are arrays.

但是我强烈推荐使用std::array方法。它不太容易出错和误用。用户不会知道VERTREX或TRIANGLE是数组。

#2


3  

typedefs are aliases. 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++. You cannot assign to an array like that.

这在C/ c++中是不允许的。不能像这样给数组赋值。

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 TRIANGLEs, you'll have to use a similar strategy to copy a TRIANGLE to an element of the array.

当您使用一个三角形数组时,您必须使用类似的策略将一个三角形复制到数组的一个元素中。


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 

#3


1  

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

//用visualc++ (v.120)进行编译,警告级别为4,如果删除行(1),则没有警告,这里输出:41 41。

#4


0  

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