c++如何自定义一个返回二维数组的函数而不是首地址?

关注者
31
被浏览
9,217

4 个回答

不可以。只能用 struct/class 包装,或是使用C++11的std::array。

#include <iostream>
using namespace std;
struct Matrix {
    float m[2][2];
Matrix Identity() {
    Matrix m = { { { 1, 0 }, { 0, 1 } } };
    return m;
int main()
    Matrix i = Identity();
    cout << i.m[0][0] << " " << i.m[0][1] << endl; 
    cout << i.m[1][0] << " " << i.m[1][1] << endl; 
   return 0;

--------

更新:C++11的std::array

#include <iostream>
#include <array>
using namespace std;
using Matrix = std::array<std::array<float, 2>, 2>;
Matrix Identity() {
    Matrix m = { { { 1, 0 }, { 0, 1 } } };
    return m;
int main()
    Matrix i = Identity();
    cout << i[0][0] << " " << i[0][1] << endl; 
    cout << i[1][0] << " " << i[1][1] << endl; 
   return 0;