简体   繁体   中英

Function that returns multidimensional array in C++

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;
}

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.

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.

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.

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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