实施[B,C] = F(A)的语法(函数f作用在阵列上具有两个或更多的输出阵列) [英] Implementing the [B,C]=f(A) syntax (function f acting on an array with two or more output arrays)

查看:120
本文介绍了实施[B,C] = F(A)的语法(函数f作用在阵列上具有两个或更多的输出阵列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题这是另外两个问题的扩展我最近已经发布:

I have a question which is an extension of other two questions I have recently posted:

<一个href=\"http://stackoverflow.com/questions/16254797/implementing-b-fa-with-b-and-a-arrays-and-b-already-defined\">Implementing B = F(A),与B和A阵列和B已经

<一个href=\"http://stackoverflow.com/questions/16287644/implementing-the-b-fa-syntax-by-move-assignment\">Implementing在B = F(A)由移动分配的语法

假设我有一个数组 A 。我想创建一个函数˚F作用于 A 并返回其他两个数组 B C ,通过启用以下Matlab的语法类似

Suppose I have an array A. I want to create a function f that acts on A and returns two other arrays B and C, by enabling the following Matlab-like syntax

[B,C]=f(A);

在C有可能++?

Is it possible in C++?

解决方案以下列出LEEMESANSWER

#include <tuple>
using std::tie;

std::tuple<TypeOfB,TypeOfC> f(const Matrix<T1>&a,const Matrix<T2>&a) {

    // Instruction declaring and defining B_temp and C_temp

    return std::make_tuple(B_temp,C_temp); }

int main( int argc, char** argv) 
{

    // Instruction declaring A, B and C

    tie(B,C)=f(A);

    // STUFF

    return 0;

 }

一切工作也在发生变化时,的std ::元组 make_tuple 的std ::对的std :: make_pair <​​/ code>对于这种特殊情况下(只有两个输出)。

Everything works also when changing std::tuple and make_tuple to std::pair and std::make_pair for this particular case (only two outputs).

推荐答案

在一般情况下,如果要返回多个值,你必须做一些小的解决方法,因为C ++不允许这种开箱。

In general, if you want to return multiple values, you have to do some little work-around, since C++ doesn't allow this out of the box.

第一个选项是返回 STD ::对 包含两个值。然后你可以使用 的std ::领带 如果您有C ++ 11可用,这样一个return语句:

The first option is to return a std::pair containing both values. Then you can use std::tie in a return statement if you have C++11 available, like this:

std::tie(B, C) = f(A);

(注:C ++ 11还具有 的std ::元组 超过两个值。)

(Note: C++11 also has std::tuple for more than two values.)

或者你也可以通过引用传递两个目标变量,但随后的函数调用变得像这样(没有工作C ++ 11):

Or you can pass the two target variables by reference, but then the function call becomes something like this (works without C++11):

f(A, B, C);

为了使函数调用看起来更加详细(有些人不喜欢,你不能告诉˚F修改 B C ),您也可以使用指针而不是引用。然后,函数调用看起来是这样的:

To make the function call look more "verbose" (some people don't like that you can't tell that f changes B and C from looking at this single line of code) you can also use pointers instead of references. Then the function call would look like this:

f(A, &B, &C);

另一种选择是使用一个简单的容器您的多个返回值。如果一个简单的对或元组不给值的特殊意义,这是特别有用的。最好的办法是在code这就要求坚持用这个˚F(不要使用单独的数组 B C )。使用到code设计的其余部分仅当它适合很好。

Another option is to use a simple "container" for your multiple return values. This is useful in particular if a simple pair or tuple don't give the values a particular meaning. The best option is to use this consistently in the code which calls f (don't use separate arrays B and C). Use this only if it fits nicely into the rest of your code design.

struct TwoArrays {
    int B[100];
    int C[100];
};

TwoArrays result = f(A);

这篇关于实施[B,C] = F(A)的语法(函数f作用在阵列上具有两个或更多的输出阵列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆