简体   繁体   English

从函数返回值:reference vs struct

[英]Returning values from a function : reference vs struct

I'm new to programming and I'm studying the C++ programming language using the book : Programming principles and practice using C++. 我是编程新手,我正在使用这本书学习C ++编程语言:使用C ++编程原理和实践。 I'm here today because at the end of chapter 8, the author focuses on functions and proposes an exercise to invite the learner to think about the better solution to a problem : 我今天在这里,因为在第8章末尾,作者关注功能并提出一个练习,邀请学习者思考问题的更好解决方案:

Write a function that finds the smallest and the largest element of a vector argument and also computes the mean and the median. 编写一个函数,查找向量参数的最小和最大元素,并计算均值和中位数。 Do not use global variables. 不要使用全局变量。 Either return a struct containing the results or pass them back through reference arguments. 返回包含结果的struct或通过引用参数传回它们。 Which of the two was of returning several values do you prefer and why ? 您更喜欢哪两个返回多个值?为什么?

Now, usually I wouldn't define a single function to perform several actions but in this case I have to create just one function and think about how to return several values. 现在,通常我不会定义单个函数来执行多个操作,但在这种情况下,我必须只创建一个函数并考虑如何返回多个值。 My first approach was to create a function that takes reference arguments like this : 我的第一个方法是创建一个带有这样的引用参数的函数:

void my_func(
    vector<double>& numbers,
    double& min,
    double& max,
    double& mean,
    double& median
);

But going on with writing the program I started to think that this solution used too many arguments and maybe the other solution proposed (using struct ) would be better. 但继续编写程序我开始认为这个解决方案使用了太多的参数,也许提出的其他解决方案(使用struct )会更好。 How would you use a struct to solve this problem ? 你会如何使用struct来解决这个问题? How do you return several values from a function ? 如何从函数中返回多个值?

Using struct for this problem is simple: define a struct for the return type, and use it, like this: 使用struct来解决这个问题很简单:为返回类型定义一个struct ,并使用它,如下所示:

struct stats {
    double min;
    double max;
    double mean;
    double median;
};
stats my_func(vector<double>& numbers) {
    stats res;
    ...
    res.min = ...
    res.max = ...
    res.mean = ...
    res.median = ...
    return res;
}

The tradeoff here is that in exchange for having a much simpler function signature, the users of your function need to extract the elements that they want one by one. 这里的权衡是,为了换取更简单的函数签名,函数的用户需要逐个提取他们想要的元素。

But what about if the struct is really complex and the cost of copying becomes too expensive? 但是,如果结构非常复杂并且复制成本太昂贵呢?

It takes a structure of extreme size for copying to become too expensive in comparison to the "payload" CPU time of your function. 与函数的“有效负载”CPU时间相比,复制时需要一个极大的结构才能变得过于昂贵。 On top of that, C++ optimizer reduces the copying costs by employing copy elision and return value optimization strategies when you do this: 最重要的是,当您执行此操作时,C ++优化器通过使用复制省略返回值优化策略来降低复制成本:

stats s = my_func(numbers);

When the struct becomes so gigantic that you don't want to copy it, combine the two approaches like this: struct变得如此巨大以至于你不想复制它时,将这两种方法结合起来:

void my_func(vector<double>& numbers, stats& res);

Declare the struct 声明struct

ie

struct stats {
   double min;
   double max;
   double mean;
   double median;
};

Then 然后

stats my_func(vector<double>& numbers);

The function will be like this 功能将是这样的

stats my_func(vector<double>& numbers) {
    stats return_value;

     // Fill in the structure

    return return_value;
}

Well you just return a struct. 那么你只需返回一个结构。

struct res {
   double min; 
   double max;
   double mean;
   double median;
};

res my_func(vector<double>& numbers) {
    res result;
    // do stuff, put result in in result.min etc

    return result;
}

The other can be done in a similar way: 另一种可以以类似的方式完成:

void my_func(vector<double>& numbers,
             res& result)
{
    // the same code except no return value

    return;
}

Now for the question in your task (which I shall not answer - because it's a question what you prefer) I'd like to mention what the technical difference is. 现在针对您的任务中的问题(我不会回答 - 因为这是您更喜欢的问题)我想提一下技术差异是什么。

When returning a struct it means you will possibly create a temporary copy of the result struct (it may also result in two objects res one for the caller and one for my_func to work with). 当返回一个结构就意味着你可能会创建的临时副本result结构(这也可能会导致两个对象res一个呼叫者和一个用于my_func与工作)。 While the other approach means that you pass to the function essentially addresses where to put the result. 而另一种方法意味着您传递给函数本质上解决了放置结果的位置。 With a "good" implementation you may end up with the same code. 使用“良好”实现,您可能会得到相同的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM