在C ++中返回多维数组的函数

时间:2021-12-09 21:37:01

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

作为一名Java程序员,我很难获得一个返回多维数组的函数。我将如何在C ++中编写代码?:

int[][] theFunction(){
  int[][] var = new int[3][3];
  // code
  return var;
}

5 个解决方案

#1


6  

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

在C ++中,最简单的方法是这样的:

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.

您需要包含 才能进行编译。与Java通用容器不同,C ++模板容器不会产生将基元包装到对象中的成本,因此它们在性能和内存消耗方面可以保持极其高效,同时提供大量额外的灵活性。

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.

一般来说,你应该更喜欢C ++风格的容器(std :: vector,std :: set,std :: map,以及在C ++ 11,std :: array中),而不是灵活的内置替代品“继承” C。

#2


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).

简而言之,在C中(比方说,你使用的是C ++中的方言[C ++是C的超集,有一些修改])你不能从函数中返回向量和矩阵。但是,您可以返回指针(可能这对您没有帮助)。

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

在C和C ++中,向量的名称(让我们简化它)是指向第一个位置的指针,因此:

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解决的问题(使用新标准已经过时了,你应该在编译器允许的情况下使用unique_ptr)。

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.

在这个方向上工作的是C ++标准库的一部分,你可以使用vector <>模板,它更安全,更安全。

Hope this helps.

希望这可以帮助。

#3


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.

正如其他人所说,你可以返回一个std :: vector >。值得指出的是,这是一个C ++的返回值优化非常重要的情况 - 特别是,只要查看这段代码,您可能会认为在函数返回时必须复制整个向量向量(从C ++开始)函数的返回值由值返回)。但是这种由C ++明确允许并且几乎由任何编译器实现的优化允许函数在将要返回的任何结构中分配向量。

#4


0  

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

#5


0  

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

你不会在C ++中使用C数组(在公式上看起来像Java数组),你可以使用vector:

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

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

#1


6  

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

在C ++中,最简单的方法是这样的:

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.

您需要包含 才能进行编译。与Java通用容器不同,C ++模板容器不会产生将基元包装到对象中的成本,因此它们在性能和内存消耗方面可以保持极其高效,同时提供大量额外的灵活性。

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.

一般来说,你应该更喜欢C ++风格的容器(std :: vector,std :: set,std :: map,以及在C ++ 11,std :: array中),而不是灵活的内置替代品“继承” C。

#2


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).

简而言之,在C中(比方说,你使用的是C ++中的方言[C ++是C的超集,有一些修改])你不能从函数中返回向量和矩阵。但是,您可以返回指针(可能这对您没有帮助)。

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

在C和C ++中,向量的名称(让我们简化它)是指向第一个位置的指针,因此:

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解决的问题(使用新标准已经过时了,你应该在编译器允许的情况下使用unique_ptr)。

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.

在这个方向上工作的是C ++标准库的一部分,你可以使用vector <>模板,它更安全,更安全。

Hope this helps.

希望这可以帮助。

#3


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.

正如其他人所说,你可以返回一个std :: vector >。值得指出的是,这是一个C ++的返回值优化非常重要的情况 - 特别是,只要查看这段代码,您可能会认为在函数返回时必须复制整个向量向量(从C ++开始)函数的返回值由值返回)。但是这种由C ++明确允许并且几乎由任何编译器实现的优化允许函数在将要返回的任何结构中分配向量。

#4


0  

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

#5


0  

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

你不会在C ++中使用C数组(在公式上看起来像Java数组),你可以使用vector:

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

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