开发者

How to return more than one value from a C++ function?

开发者 https://www.devze.com 2023-04-03 09:49 出处:网络
I am interested if I can return more then one value from a function. For example consider such a function: extended euclidean algorithm. The basic step is described by this

I am interested if I can return more then one value from a function. For example consider such a function: extended euclidean algorithm. The basic step is described by this Input is nonnegative integers a and b; output is a triplet (d,i,j) such that d=gcd(a,b)=i*a+j*b. Just to clarify my question's goal I will write a 开发者_如何学Cshort recursive code:

 if (b==0)  return (a,1,0)
      q=a mod b;

let r be such that a=r*b+q;

(d,k,l)=extendedeuclidean(b,q);
  return (d,l,k-l*r); 

How does one return a triplet?


You could create a std::tuple or boost::tuple (if you don't use C++0x) from your triple pair and return that.


As has been suggested by Tony The Tiger you can use tuple. It is included in C++11 standard and new compilers already support it. It is also implemented in boost. For my ibm xlC compiler tuple is in std::tr1 namespace (tried it for MSVC10 — it's in std namespace).

#include <cstdio>
#include <tuple>

// for MSVC
using namespace std;

// for xlC 
//using namespace std::tr1;

// for boost
// using namespace boost;

typedef tuple<int, float, char> MyTuple;
MyTuple f() {
    return MyTuple(1, 2.0f, '3');
}

int main() {
    MyTuple t = f();
    printf("%i, %f, %c\n", get<0>(t), get<1>(t), get<2>(t));
}

xlC compilation for TR1:

xlC -D__IBMCPP_TR1__ file.cpp

xlC compilation for boost:

xlC file.cpp -I/path/to/boost/root


Just create an appropriate data structure holding the three values and return that.

struct extmod_t {
    int d;
    int i;
    int j
    extmod_t(int d, int i, int j) : d(d), i(i), j(j) { }
};

…

extmod_t result = extendedeuclidean(b, q);
return extmod_t(result.d, l, k - l * r);


Either create a class that encapsulates the triplet and then return the instance of this class, or use 3 by-reference parameters.


I usually find that when I need to return two parameters from a function, it is useful to use the STL std::pair.

You could always stack the pairs inside one another (e.g. std::pair <int, std::pair <int, int> >) and help your self with typedef-s or defines to make it more accessible, but when ever I try doing this my code ends up messy and unpractical for re-use.

For more than two parameters, however, I recommend making you own specific data structure that holds the information you need (if you are returning multiple values there's a high likelihood that they are strongly logically connected somehow and that you might end up using the same structure again).

E.g. I needed a function that returned the slope of the line (1 param) and that was fine. Then I needed to expand it to return both parameters of the parametric representation of the line (y = k*x + l). Two parameters, still fine. Then I remembered that the line can be vertical and that I should add another parameter to indicate that (no parametric representation then)... At this point, it became too complicated to try and make do with existing datatypes, so I typed up my own Line structure and ended up using the same structure all over my project later.

0

精彩评论

暂无评论...
验证码 换一张
取 消