用C ++将结构数组展平成几个数组

时间:2013-04-05 04:18:20

标签: c++ arrays struct flatten

了解情况的源代码:

struct s { 
    int i; 
    float f 
};
const int cnt = 10;

s *source = new s[cnt];
/*... fill source ...*/

int *dest_i = new int[cnt];
float *dest_f = new float[cnt];

for (int x = 0; x < cnt; x++) {
    dest_i[x] = source[x].i;
    dest_f[x] = source[x].f;
}

所以,这里有一个问题:是否有比使用循环迭代数组更快的方法?

1 个答案:

答案 0 :(得分:1)

您可以展开循环。这就是我能想到的全部。

编写自己是一个相当无意义的优化,编译器可以尝试为你启用它(在gcc中,用--funroll-loops编译)