使用数组,结构或类有速度差异吗?

Nik*_*s R 2 c++ performance

我需要根据变量anmount数据进行计算,数据中的每个项目包含3个值.我可以使用数组,结构来表示其中一个项目.

速度有什么不同,或者它们的行为方式是否相同?

// #1: Only arrays
typedef int triple[3];

// #2: Using a struct
struct triple {
    int a;
    int b;
    int c;
};

// #3: Using a class
class triple {
public:
    int a;
    int b;
    int c;
};
Run Code Online (Sandbox Code Playgroud)

smp*_*kes 7

结构和类是尽可能相同的.只要你使用常量索引,所有的数学运算都是在编译时完成的,所以它不应该有任何区别.