0

I want to return multiple vectors from a function. I am not sure either tuple can work or not. I tried but is not working.

xxx myfunction (vector<vector<float>> matrix1 , vector<vector<float>> matrix2) {

// some functional code: e.g. 
// vector<vector<float>> matrix3 = matrix1 + matrix2;
// vector<vector<float>> matrix4 = matrix1 - matrix2;

return matrix3, matrix4;
aly
  • 17
  • 5
  • 1
    Is there a reason you couldn't create a class that houses two vectors, and `return` one of those instead? – Bryn McKerracher Jan 07 '15 at 04:58
  • In my opinion, it would be more intuitive to have a function returning just one thing, rather than many. Even if you could get it to work, it would be more confusing since you're dealing with matrices especially. Let it suffice that your approach is bug-prone. Have a function for matrix addition, and one for matrix subtraction. – Poriferous Jan 07 '15 at 05:00
  • Thank you for your replies. Actually I am translating my code from python to c++ and I need to return 2 matrices according to my application. – aly Jan 07 '15 at 15:53

2 Answers2

1

If these matrices are very small then this approach might be OK, but generally I would not do it this way. First, regardless of their size, you should pass them in by const reference.

Also, std::vector<std::vector<T>> is not a very good "matrix" implementation - much better to store the data in a contiguous block and implement element-wise operations over the entire block. Also, if you are going to return the matrices (via a pair or other class) then you'll want to look into move semantics as you don't want extra copies.

If you are not using C++11 then I'd pass in matrices by reference and fill them in the function; e.g.

using Matrix = std::vector<std::vector<float>>; // or preferably something better

void myfunction(const Matrix &m1, const Matrix &m2, Matrix &diff, Matrix &sum)
{
    // sum/diff clear / resize / whatever is appropriate for your use case
    // sum = m1 + m2
    // diff = m1 - m2
}

The main issue with functional style code, e.g. returning std::tuple<Matrix,Matrix> is avoiding copies. There are clever things one can here to avoid extra copies but sometimes it is just simpler, IMO, to go with a less "pure" style of coding.

sfjac
  • 7,119
  • 5
  • 45
  • 69
  • First, I made a class to generate and perform matrix operation using the vector library; then I am using it in another class; and finally I have to return two matrices from second class to main. I do not understand from your code that how can I perform this task of returning two matrices. – aly Jan 07 '15 at 16:01
  • 1
    @aly, can you provide us with details of what exactly that function in second class does, so that it requires to return two matrices? The reason I am asking is, if the operations on each of the matrices are modular (i.e data from first input matrix is not used for operations on the second input matrix) then you can split the function into two ( http://www.codeproject.com/Articles/6154/Writing-Efficient-C-and-C-Code-Optimization) to keep it small and simple. Also it is a good idea to rethink design, when dealing with implementation complexities. – Swtsvn Jan 08 '15 at 03:19
  • that function in second class takes 4 matrices as input and manipulate them according to my equations and generate 2 matrices and an integer value which are interrelated to each other. SO i need to return these three values. I can separate them but my code will be too bulky. That's why i want to return multiple values from a function. Thanks – aly Jan 08 '15 at 10:58
0

For Matrices, I normally create a Struct or Class for it that has these vectors, and send objects of that class in to the function. It would also help to encapsulate Matrix related operations inside that Class.

If you still want to use vector of vector, here is my opinion. You could use InOut parameters using references/pointers : Meaning, if the parameters can be updated to hold results of calculation, you would be sending the arguments in, and you would not have to return anything in that case.

If the parameters need to be const and cannot be changed, then I normally send In parameters as const references, and separate Out parameters in the function argument list itself.

Hope this helps a bit.

Swtsvn
  • 315
  • 2
  • 13
  • Thanks Swtsvn. Actually I have designed a separate class for matrix operation to use in another class. I need to return two matrices at a time. Your approach for creating a Class or Struct seems understandable, can you write an example? – aly Jan 07 '15 at 15:56
  • Vector of vectors is not the optimal approach, since it does dynamic allocations, when compared to static block of memory of an array ( http://stackoverflow.com/questions/17005704/why-is-it-bad-to-use-vectors-when-creating-a-matrix-class ). You can use old school way of using 2d array class Matrix3x3{ private: float[3][3] data; public: ... // your operator overloads go here }; // more code details: ftp://ftp.cs.uregina.ca/pub/class/170/06-2Darrays/index.html~ or you can use vector for each row or column class Matrix3x3{ private: vector row1; //row2,3,4 }; Hope this helps. – Swtsvn Jan 08 '15 at 03:12