C++ 中将一维数组转成多维的三种方式示例详解

 更新时间:2023年12月26日 10:08:37   作者:恒友成  
这篇文章主要介绍了C++ 中将一维数组转成多维的三种方式,每种方式结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

使用reinterpret_cast

reinterpret_cast作用为:允许将任何指针转换为任何其他指针类型。

#include <iostream>
int main() {
    int arr4[2] = {0, 12};
    int* p5 = reinterpret_cast<int*>(&arr4);
    cout << p5[1] << std::endl; 
    struct S1 { int a; } s1;
    s1.a  = 100;
    int* p1 = reinterpret_cast<int*>(&s1);
    cout << *p1;   
}
// 12
// 100

上面例子中,p1 是指向s1.a的指针,s1.as1是指针可互换的。

如何将一维指针转换成多维指针呢?看一个例子,

#include <iostream>
using namespace std;
int main()
{
    constexpr const unsigned long n = 8;
    float arr1[n] = {1.2f, 1.3f, 1.4f, 1.5f, 1.6f, 1.7f, 1.8f, 1.9f};
    float *arr = new float[n];
    for(int i = 0; i < n; i++) {
        arr[i] = arr1[i];
    }
    // 1.reinterpret_cast
    unsigned long I, J, K;
    I = 2;
    J = 2;
    K = 2;
    cout << arr[0] << endl;
    float (&arr3d)[I][J][K] = *reinterpret_cast<float (*)[I][J][K]>(arr);
    cout << arr3d[0][0][0] << endl;
}

float (*)[I][J][K]是数组指针类型,指向形如[I][J][K]数组的首地址,reinterpret_cast后将这个转换结果赋值给了arr3d

强制类型转换

float(*arr3d1)[I][J] = (float(*)[I][J])arr;
cout << arr3d1[0][0][0] << endl;

这里是将arr转换成float(*)[I][J]的数组指针,arr3d1相当于指向[I][J][K]数组的行指针[I][J][0],这里访问一定要知道arr指针中的元素个数,防止越界。

使用static_cast

static_cast操作符可用于将一个指向基类的指针转换为指向子类的指针。但是这样的转换不总是安全的。static_cast不够安全,就是指在运行阶段不进行类型检查。其主要作用是:

  • 基类(父类)和派生类(子类)之间指针或引用的转换
  • void指针转换成目标类型的指针
  • 数字类型转换,一般情况下,可以使用static_cast用于数字类型的转换,如把枚举转换为int或是 float类型。
float (&arr3d2)[I][J][K] = *static_cast<float (*)[I][J][K]>(static_cast<void*>(arr));
cout << arr3d2[0][0][0] << endl;

上面例子中先将float*转换成了void*,然后将void*转换成了float (*)[I][J][K]类型,再取指针变量的值作为数组arr3d2的引用,如此就完成了类型转换。看一个简单的例子:

float c[4] =  {1,2,3,4};
float (&b)[4] = *static_cast<float(*)[4]>(&c); 
cout<<b[1]<<endl;

上面例子b是一个数组变量的引用,float(*)[4]是指向数组的指针,*static_cast<float(*)[4]>是取指针的值,也就是数组的首地址,b正是这个首地址的引用。

转换成多维数组时,之所以需要先转成void*static_cast转换约束的限制,如果使用reinterpret_cast它可以强制性的将一个指针类型转换成另一种不会做检查,因此就不需要先转成void*

float c[4] =  {1,2,3,4};
float (&b)[2][2] = *static_cast<float(*)[2][2]>(static_cast<void*>(&c)); 
float (&d)[2][2] = *reinterpret_cast<float(*)[2][2]>(&c); 
cout<<b[1][1]<<endl;

从这里也能看出,static_cast类型转换是有保护的,如果同样能使用static_castreinterpret_cast时,应优先使用static_cast.

1.https://stackoverflow.com/questions/310451/should-i-use-static-cast-or-reinterpret-cast-when-casting-a-void-to-whatever

到此这篇关于C++ 中将一维数组转成多维的三种方式的文章就介绍到这了,更多相关C++ 维数组转成多维内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论