5

Being a Java-programmer, I have a hard time getting a function to return a multidimensional array. How would i code this in C++?:

int[][] theFunction(){
  int[][] var = new int[3][3];
  // code
  return var;
}
1
  • 1
    Does this work? Have you tried it? Could the return be **int? Commented Dec 20, 2012 at 17:51

5 Answers 5

6

In C++, the easiest way of doing it is like this:

std::vector<std::vector<int> > theFunction() {
    std::vector<std::vector<int> > var(3, std::vector<int>(3));
    // code
    return var;
}

You need to include <vector> in order for this to compile. Unlike Java generic containers, C++ template containers do not incur the cost of wrapping primitives into objects, so they can stay extremely efficient in terms of performance and memory consumption while providing a great deal of additional flexibility.

In general, you should prefer C++ - style containers (std::vector, std::set, std::map, and in C++11, std::array) to their less flexible built-in alternatives "inherited" from C.

Sign up to request clarification or add additional context in comments.

Comments

2

In short, in C (which is the, let's say, dialect you're using from C++ [C++ is a superset of C, with some modifications]) you cannot return a vector nor matrix from a function. You can, though, return a pointer (and probably that's not going to help you very much).

In C and C++, the name of the vector (let's simplify it) is a pointer to the first position, so:

int v[] = { 1, 2, 3 };
int * ptr = v;

A pointer is a memory address, you can use that in order to run over all elements (though it can be dangerous):

for( ; ptr < ( v + 3 ); ++ptr) {
     std::cout << *ptr << stD::endl;
}

And that pointer can be returned:

int * vectorCreator(int max)
{
    int * v = new int[max];
    return v;
}

Beware that, in this case, the caller takes the responsibility of freeing the vector once is done. This is a problem you can solve with auto_ptr (which is obsolete with the new standard, you should use unique_ptr once your compiler allows it).

auto_ptr<int> vectorCreator(int max)
{
    int * v = new int[max];
    return auto_ptr<int>( v );
}

In this very direction works part of the C++ standard library, in which you can use the vector<> template, which is safer, and definitely more comfortable.

Hope this helps.

Comments

1

As others have said, you can return a std::vector<std::vector<int> >. It's worth pointing out that this is a case where C++'s Return Value Optimization can be very important -- in particular, just looking at this code, you might think that the entire vector-of-vectors must get copied when the function returns (since C++ functions' return values are returned by value). But this optimization, which is explicitly permitted by C++ and is implemented by almost any compiler, allows the function to allocate the vector in whatever structure it will be returned into.

Comments

0
std::vector<std::vector<int> > theFunction() {
    std::vector<std::vector<int> > var;
    // code
    return var;
}

Comments

0

You wouldn't use C arrays (which notationally look like Java arrays) in C++, you'd use vector:

typedef std::vector<std::vector<int> > VecType;

VecType theFunction()
{
  VecType var(3, std::vector<int>(3));
  // code
  return var;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.