【问题标题】:Get more than one value out of an function [duplicate]从函数中获取多个值[重复]
【发布时间】:2011-12-11 07:40:38
【问题描述】:

可能重复:
Returning multiple values from a C++ function

/************************************************/
/* Name: premserv                               */
/* Description: Calculations for premium serv   */
/* Parameters: N/A                              */
/* Return Value: premserv                       */
/************************************************/

float premserv ()
{
  int daymin = 0;         //Var for day minutes
  int nightmin = 0;       //Var for night minutes
  float daytotal = 0;     //Var for day total
  float nighttotal = 0;   //Var for night total
  float premserv = 0;     //Var for premium service cost

  cout << "\n" << "Please enter the number of minutes used durring the day (6AM - 6PM): " << "\n";
  cin >> daymin;
  cout << "\n" << "Please enter the number of minutes used durring the night (6PM - 6AM): " << "\n";
  cin >> nightmin;

  daytotal = (daymin - 75) * 0.1;
  nighttotal = (nightmin - 100) * 0.05;
  premserv = 25 + daytotal + nighttotal;

  return premserv;

}

我需要从函数中获取 daymin、nightmin 和 premserv 的值。我只知道如何从函数中获取 1 个值。

【问题讨论】:

  • 请不要在每个问题的标题中都以“c++”开头。这已经在标签中了。谢谢。
  • @Tomalak Geret'Kal:好的,非常感谢您的提醒

标签: c++


【解决方案1】:

创建一个包含所有三个浮点数的结构并返回它而不是单个浮点数。

【讨论】:

    【解决方案2】:

    你可以使用references

    float premserv (int& daymin, int& nightmin)
    {
         float daytotal = 0;     //Var for day total
         float nighttotal = 0;   //Var for night total
         float premserv = 0;     //Var for premium service cost
    
         cout << "\n" << "Please enter the number of minutes used durring the day (6AM - 6PM): " << "\n";
         cin >> daymin;
         cout << "\n" << "Please enter the number of minutes used durring the night (6PM - 6AM): " << "\n";
         cin >> nightmin;
    
         daytotal = (daymin - 75) * 0.1;
         nighttotal = (nightmin - 100) * 0.05;
         premserv = 25 + daytotal + nighttotal;
    
         return premserv;
    }
    

    这将直接修改dayminnightmin 的值,您仍然可以将premserv 作为返回值(因为它与函数名称相符,我猜)。

    你会这样使用它:

    int daymin = 0;
    int nightmin = 0;
    float premiumservice = premserv(daymin, nightmin);
    

    【讨论】:

    • 我需要在 int main 中声明这些变量吗?
    • @SamLaManna:是的,它们必须存在,这样您才能将引用传递给它们。
    • (我通常不会做这么主观的编辑,但我觉得this one is important for language newcomers很喜欢OP。)
    • 变量名和函数名一样吗?嗯……>.
    • 它只是返回值,我总是将它分配给 int main 中的不同 var 名称。 premserv 成为高级服务
    【解决方案3】:

    在 c 中,创建一个结构来保存上述 3 个值。在 c++ 中创建一个类并返回该类的一个对象。

    【讨论】:

    • 你说了两次同样的话。
    【解决方案4】:
    float premserv(int& daymin, int& nightmin) { 
        float daytotal = 0;     //Var for day total    
        float nighttotal = 0;   //Var for night total    
        float premserv = 0;     //Var for premium service cost    
        // etc...
    

    'premserv' 已被返回。

    【讨论】:

      【解决方案5】:

      你得到了多个答案,但或者你可以在函数参数中返回结构,这样你就有一个对象要处理,返回值会告诉你数据是否有效。

      struct
      {
         float premserv;
         int daymin;
         int nightmin;
      
      } data;
      
      bool premserv (data & my_data)
      {
         // ...
      }
      
      if( premserv(my_data) == true )
      {
        // user the data
      }
      

      【讨论】:

        【解决方案6】:

        除了使用 out 参数或定义特定的结构返回类型之外,另一个选择是使用 C++11 或 TR1 中的元组:

        /* Return Value: tuple<daymin,nightmin,premserv>   */
        /***************************************************/
        
        std::tuple<int,int,float> premserv ()
        {
          // ...
          return std::make_tuple(daymin, nightmin, premiumservice);
        }
        

        那么你可以这样使用它:

        int daymin, nightmin;
        float premiumservice;
        std::tie(daymin,nightmin,premiumservice) = premserv();
        

        或者,如果您只关心其中一个值:

        int nightmin = std::get<1>(premserv());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-23
          • 2020-06-20
          • 2011-04-19
          • 2021-12-24
          相关资源
          最近更新 更多