C++ 将char****作为连续数据复制到char*变量

C++ 将char****作为连续数据复制到char*变量,c++,pointers,C++,Pointers,如何在不使用嵌套循环的情况下将所有内容从char****first复制到连续变量:char*second 例如,我可以使用嵌套For循环,如下所示: for (size_t a=0; a < sizeof(***first); a++) { for (size_t b=0; b < sizeof(**first); b++) { //etc } } for(大小a=0;a

如何在不使用嵌套循环的情况下将所有内容从
char****first
复制到连续变量:
char*second

例如,我可以使用嵌套For循环,如下所示:

for (size_t a=0; a < sizeof(***first); a++) {
    for (size_t b=0; b < sizeof(**first); b++) {
      //etc
    }
}
for(大小a=0;a

我只是想知道这是否可行。

如果您知道多维数组的各个大小,可以在单个循环中展平数组。但应该注意的是,与使用一些嵌套循环相比,在1个循环中展平一个数组可能并没有实际看到性能的提高;如果您必须确定每个嵌套数组的大小,那么它也可能取决于您的编译器优化(如向量化)和平台/体系结构,因为在大循环的顶部,您将受到性能的影响。因此,最好做一些小规模的基准测试,以验证它是否达到了您想要的结果和性能

下面是一些关于如何展平数组的示例代码;请注意,在本例中,我使用了
std::string
类型,这样您就可以运行代码并打印到文件/stdout中,以查看数组确实已被展平

#include <iostream>
#include <string>
#include <sstream>

void flatten(std::string**** first, int len_a, int len_b, int len_c, int len_d, std::string* second)
{
    int i, a, b, c, d;
    int max_len = len_a * len_b * len_c * len_d;
    a = b = c = d = -1;
    for (i = 0; i < max_len; ++i) {
        d = (i % len_d);
        // if all lengths were equal, you could further optimize this loop
        if (d == 0) {
            ++c;
            if ((c % len_c) == 0) {
                c = 0;
                ++b;
                if ((b % len_b) == 0) {
                    b = 0;
                    ++a;
                }
            }
        }
        second[i] = first[a][b][c][d];
    }
}

int main()
{
    const int len_a = 11;
    const int len_b = 22;
    const int len_c = 33;
    const int len_d = 44;
    const int max_len = len_a * len_b * len_c * len_d;
    // create the arrays
    std::string**** first = new std::string***[len_a];
    std::string* second = new std::string[max_len];
    for (int i1 = 0; i1 < len_a; ++i1) {
        first[i1] = new std::string**[len_b];
        for (int i2 = 0; i2 < len_b; ++i2) {
            first[i1][i2] = new std::string*[len_c];
            for (int i3 = 0; i3 < len_c; ++i3) {
                first[i1][i2][i3] = new std::string[len_d];
                for (int i4 = 0; i4 < len_d; ++i4) {
                    std::stringstream ss;
                    ss <<"["<<i1<<"]["<<i2<<"]["<<i3<<"]["<<i4<<"]";
                    first[i1][i2][i3][i4] = ss.str(); // or what have you
                }
            }
        }
    }

    // flatten the multidimensional array 'first' into the array 'second'
    flatten(first, len_a, len_b, len_c, len_d, second);

    // print it
    for (int i1 = 0; i1 < max_len; ++i1) {
        std::cout<<"second["<<i1<<"] = "<<second[i1]<<std::endl;
    }

    // clean up
    delete[] second;
    for (int i1 = 0; i1 < len_a; ++i1) {
        for (int i2 = 0; i2 < len_b; ++i2) {
            for (int i3 = 0; i3 < len_c; ++i3) {
                delete[] first[i1][i2][i3];
            }
            delete[] first[i1][i2];
        }
        delete[] first[i1];
    }
    delete[] first;
    return 0;
}
#包括
#包括
#包括
无效展平(std::string****第一,int len_a,int len_b,int len_c,int len_d,std::string*第二)
{
int i、a、b、c、d;
int max_len=len_a*len_b*len_c*len_d;
a=b=c=d=-1;
对于(i=0;iss如果您知道多维数组的各个大小,您可以在单个循环中展平数组。但是应该注意的是,在1个循环中展平数组与使用一些嵌套循环相比,实际上可能看不到性能的提高;这可能取决于您的编译器优化(如向量化)还有平台/体系结构,如果您必须确定每个嵌套阵列的大小,那么在大循环.YMMV的基础上,您将受到性能的影响,因此最好进行一些小规模的基准测试,以验证它是否达到了您想要的结果和性能

下面是一些关于如何展平数组的示例代码;请注意,我在这个示例中使用了
std::string
类型,这样您就可以运行代码并打印到文件/stdout,以查看数组确实已展平

#include <iostream>
#include <string>
#include <sstream>

void flatten(std::string**** first, int len_a, int len_b, int len_c, int len_d, std::string* second)
{
    int i, a, b, c, d;
    int max_len = len_a * len_b * len_c * len_d;
    a = b = c = d = -1;
    for (i = 0; i < max_len; ++i) {
        d = (i % len_d);
        // if all lengths were equal, you could further optimize this loop
        if (d == 0) {
            ++c;
            if ((c % len_c) == 0) {
                c = 0;
                ++b;
                if ((b % len_b) == 0) {
                    b = 0;
                    ++a;
                }
            }
        }
        second[i] = first[a][b][c][d];
    }
}

int main()
{
    const int len_a = 11;
    const int len_b = 22;
    const int len_c = 33;
    const int len_d = 44;
    const int max_len = len_a * len_b * len_c * len_d;
    // create the arrays
    std::string**** first = new std::string***[len_a];
    std::string* second = new std::string[max_len];
    for (int i1 = 0; i1 < len_a; ++i1) {
        first[i1] = new std::string**[len_b];
        for (int i2 = 0; i2 < len_b; ++i2) {
            first[i1][i2] = new std::string*[len_c];
            for (int i3 = 0; i3 < len_c; ++i3) {
                first[i1][i2][i3] = new std::string[len_d];
                for (int i4 = 0; i4 < len_d; ++i4) {
                    std::stringstream ss;
                    ss <<"["<<i1<<"]["<<i2<<"]["<<i3<<"]["<<i4<<"]";
                    first[i1][i2][i3][i4] = ss.str(); // or what have you
                }
            }
        }
    }

    // flatten the multidimensional array 'first' into the array 'second'
    flatten(first, len_a, len_b, len_c, len_d, second);

    // print it
    for (int i1 = 0; i1 < max_len; ++i1) {
        std::cout<<"second["<<i1<<"] = "<<second[i1]<<std::endl;
    }

    // clean up
    delete[] second;
    for (int i1 = 0; i1 < len_a; ++i1) {
        for (int i2 = 0; i2 < len_b; ++i2) {
            for (int i3 = 0; i3 < len_c; ++i3) {
                delete[] first[i1][i2][i3];
            }
            delete[] first[i1][i2];
        }
        delete[] first[i1];
    }
    delete[] first;
    return 0;
}
#包括
#包括
#包括
无效展平(std::string****第一,int len_a,int len_b,int len_c,int len_d,std::string*第二)
{
int i、a、b、c、d;
int max_len=len_a*len_b*len_c*len_d;
a=b=c=d=-1;
对于(i=0;iss您似乎对
sizeof
的功能有严重的误解

sizeof(x)
返回对象
x
字节大小。处理指针时
sizeof(*x)
通常与
x
指向的元素数不相同

还要注意的是,在您使用的情况下,
sizeof(x)
是在编译时决定的值,
sizeof(*x)
甚至不看
x
指向什么(仅查看对象的类型
x
所指向的对象:例如
int*x=NULL;
表达式
sizeof(*x)
sizeof(int)
相同)

此外,您需要了解
int
s的多维数组之间的区别:

int x[10][10];
以及指向
int
的指针:

int **y;
即使可以使用相同的语法取消引用这两个

x[1][2] = 42;
y[1][2] = 42;
意思是完全不同的。更具体一点