假设我有
struct S {
double A;
double B;
double C;
};
and
std::vector<S> vecS(10);
我正在尝试编写一个通用函数void F(std::vector<S> vecS,structure S.x)
,使得以下计算可以发生。
F(std::vector<S> vecS, structure S.x) {
for(i=1;i<10;i++)
vecS[0].x += vecS[i].x;
// note the structure does not have memeber "x"
// I want to give that as a generic input to access A,B or C
}
上面的代码不是正确的代码,我只是试图演示我想计算什么。
我想要计算的是循环遍历一个特定元素的结构体向量。有没有一种简单而好的方法?请问有人可以给我一些指针如何以通用方式访问结构体成员(也许这就是我需要编写此函数的原因)。
提前感谢。