gpt4 book ai didi

c++ - 从函数返回 float 组

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:56 29 4
gpt4 key购买 nike

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;
}

最佳答案

假设数组的大小是固定的,使用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

关于c++ - 从函数返回 float 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28891296/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com