从函数返回浮点数组

时间:2015-03-06 02:13:02

标签: c++ function return typedef pass-by-value

findwitholding(Gross_Pay,FDH_Witholding,FW_TAXRATE);
findwitholding(Gross_Pay,FDH_Witholding,SW_TAXRATE);
findwitholding(Gross_Pay,FDH_Witholding,HOSPITALIZATION);

如何从所有三个函数中返回值?

void findwitholding(farray Gross_Pay, farray FDH_Witholding,
                float Witholding)
{          
    for(int i = 0; i < ISIZE; i++)               
        FDH_Witholding[i] = Gross_Pay[i] * Witholding;     
}

我如何通过价值传递它?喜欢这个?

typedef float farray[5] //we have to use typedef
farray FED,STATE,HOSPITAL;

 FED = findwitholding(Gross_Pay,FDH_Witholding,FW_TAXRATE);
 STATE = findwitholding(Gross_Pay,FDH_Witholding,SW_TAXRATE);
 HOSPITAL = findwitholding(Gross_Pay,FDH_Witholding,HOSPITALIZATION);

如果是这样,该功能是否应该如此?

*farray* findwitholding(farray Gross_Pay, farray FDH_Witholding,
                float Witholding)
    {          
        for(int i = 0; i < ISIZE; i++)               
            FDH_Witholding[i] = Gross_Pay[i] * Witholding;    
        return FDH_Witholding;
    }

1 个答案:

答案 0 :(得分:0)

假设数组的大小是固定的,请使用std::array

using farray = std::array<float,ISIZE>;

farray findwitholding(const farray& gross_pay, float witholding){
  farray fdh_witholding;
  for(int i = 0; i != ISIZE; ++i)
    fdh_witholding[i] = gross_pay[i] * witholding;
  return fdh_witholding;
}

auto fed = findwitholding(gross_pay, FW_TAXRATE);
auto state = findwitholding(gross_pay, SW_TAXRATE);
auto hospital = findwitholding(gross_pay, HOSPITALIZATION);

如果尺寸是动态的,请同样使用std::vector