C++ 如何将结构数据成员传递到函数中

C++ 如何将结构数据成员传递到函数中,c++,function,C++,Function,我希望能够将结构成员传递到函数: struct threeBuckets { int bucketA; int bucketB; int bucketC; }; threeBuckets allCombinations[512000] = {{0,0,0}}; int totalCombinations = 1; int counter = 0; //note that pourer, receiver, and other are one of the struct me

我希望能够将结构成员传递到函数:

struct threeBuckets {
  int bucketA;
  int bucketB;
  int bucketC;
};

threeBuckets allCombinations[512000] = {{0,0,0}};  
int totalCombinations = 1;
int counter = 0;


//note that pourer, receiver, and other are one of the struct members (bucketA, bucketB, and bucketC)

void pour(pourer, receiver, int receiverCap, other) {
  int finalTriple[3];
  allCombinations[totalCombinations].bucketA = allCombinations[counter].bucketA;
  allCombinations[totalCombinations].bucketB = allCombinations[counter].bucketB;
  allCombinations[totalCombinations].bucketC = allCombinations[counter].bucketC;
  allCombinations[totalCombinations].receiver = allCombinations[totalCombinations].receiver + allCombinations[counter].pourer;
  allCombinations[totalCombinations].pourer = 0;
  if (allCombinations[totalCombinations].receiver > receiverCap) {
    allCombinations[totalCombinations].pourer = allCombinations[totalCombinations].pourer + allCombinations[totalCombinations].receiver - receiverCap;
    allCombinations[totalCombinations].receiver = receiverCap;
  }
  finalTriple[0] = allCombinations[totalCombinations].bucketA;
  finalTriple[1] = allCombinations[totalCombinations].bucketB;
  finalTriple[2] = allCombinations[totalCombinations].bucketC;
//some more irrelevant code
}
正如我希望明确指出的,参数pourer、receiver和其他参数是bucketA、bucketB和buckeetc(没有特定的顺序,顺序确实会根据调用函数的时间而改变)。我想在几个地方修改实例

allCombinations[totalCombinations].pourer
比如说。如何使用结构成员作为参数,以及使用什么类型来指定它

注意:我基本上是一个初学者,对StackOverflow还不熟悉,所以如果我做的其他任何事情都是错误的,请随时告诉我


注2:如果你们中的任何人做过或曾经做过USACO,您可能会将此问题视为milk3培训网关问题。如果您不知道我在这里做什么,这可能会对您有所帮助。

听起来您需要对
pour
中的参数类型使用指向成员变量的指针

void pour(double threeBuckets::(*pourer) ,
          double threeBuckets::(*receiver),
          int receiverCap,
          double threeBuckets::(*other)) { 
   ...
}
在函数中,更改

allCombinations[totalCombinations].pourer
allCombinations[totalCombinations].receiver
allCombinations[totalCombinations].other

分别

在调用函数时,请使用:

pour(&threeBuckets::bucketA,
     &threeBuckets::bucketB,
     0, // Any appropriate value
     &threeBuckets::bucketC);

另一个值得考虑的选择是:

  • 更改
    threebucket
    以使用数组
  • 将参数更改为
    pour
    ,作为数组的索引

  • 然后,不用

    allCombinations[totalCombinations].pourer
    allCombinations[totalCombinations].receiver
    allCombinations[totalCombinations].other
    
    使用

    当然,将调用更改为使用索引

    pour(0,
         1
         0, // Any appropriate value
         2);
    

    听起来您需要为
    pour
    中的参数类型使用指向成员变量的指针

    void pour(double threeBuckets::(*pourer) ,
              double threeBuckets::(*receiver),
              int receiverCap,
              double threeBuckets::(*other)) { 
       ...
    }
    
    在函数中,更改

    allCombinations[totalCombinations].pourer
    allCombinations[totalCombinations].receiver
    allCombinations[totalCombinations].other
    

    分别

    在调用函数时,请使用:

    pour(&threeBuckets::bucketA,
         &threeBuckets::bucketB,
         0, // Any appropriate value
         &threeBuckets::bucketC);
    

    另一个值得考虑的选择是:

  • 更改
    threebucket
    以使用数组
  • 将参数更改为
    pour
    ,作为数组的索引

  • 然后,不用

    allCombinations[totalCombinations].pourer
    allCombinations[totalCombinations].receiver
    allCombinations[totalCombinations].other
    
    使用

    当然,将调用更改为使用索引

    pour(0,
         1
         0, // Any appropriate value
         2);
    

    我不明白,为什么不通过引用将整个结构传递给你的函数呢?通过引用传递时,开销比单独传递两个或多个成员要小。函数声明和定义中的
    pour
    函数缺少类型标识符。您的
    threebucket
    结构缺少成员
    receiver
    pourer
    。顺便说一句,您可以分配结构实例,而不必分配单个成员。我不明白,为什么不通过引用将整个结构传递给您的函数?通过引用传递时,开销比单独传递两个或多个成员要小。函数声明和定义中的
    pour
    函数缺少类型标识符。您的
    threebucket
    结构缺少成员
    receiver
    pourer
    。顺便说一句,可以指定结构实例,而无需指定单个成员。