如何迭代 C++ 类的变量成员

Wak*_*kka 5 c++ loops

我目前正在尝试对我正在读入的一些数据的一堆变量(基于在不同相空间中的标准化)进行复杂的变量校正。由于每次校正都遵循相同的过程,我想知道是否会有无论如何,要迭代地执行此操作,而不是单独处理每个变量(因为我需要对大约 18-20 个变量执行此操作)。C++ 可以处理这个吗?有人告诉我在 python 中尝试这个,但我觉得它可以在 C++ 中以某种方式完成......我只是碰壁了!

为了给你一个想法,给你一些类似的东西:

class VariableClass{
    public :
      //each object of this class represents an event for this particlular data set 
      //containing the following variables
      double x;
      double y;
      double z;
}
Run Code Online (Sandbox Code Playgroud)

我想做一些类似的事情:

for (int i=0; i < num_variables; i++)
{
   for (int j=0; j < num_events; j++)
   {
     //iterate through events
   }
   //correct variable here, then move on to next one
}
Run Code Online (Sandbox Code Playgroud)

预先感谢您的任何建议!

pio*_*kuc 1

是的,只需将所有变量放入一个容器中,例如std::vector

  • 或者`std::tuple`,如果它们有不同的类型。 (2认同)