求助矩阵

灌水区

ningago @ 2022-06-28 10:01:16

RT。求助重载运算符[]是不是这样写的?bdfs似乎没有人用类似的技巧……

struct matrix
{
    int a,b,m[1010][1010];
    void init()
    {
        for(int i = 1;i <= a;i++)
            for(int j = 1;j <= b;j++)
                m[i][j] = 0;
    }
    int* operator [](const int &x)
    {
        return m[x];
    }
};

by jijidawang @ 2022-06-28 10:06:26

我一直都是这么干的 .

或者是

int* operator [](const unsigned& x){return m[x];}
const int* operator [](const unsigned& x)const{return m[x];}

by ningago @ 2022-06-28 10:07:57

@jijidawang 所以后面是不用单独加const是吗


by fjy666 @ 2022-06-28 10:08:02

是的(?)


by ningago @ 2022-06-28 10:08:30

(蒟蒻一直写的第一种后面写const


by jijidawang @ 2022-06-28 10:09:16

@ningago 两种不容易报 CE


by ningago @ 2022-06-28 10:10:15

@jijidawang 那用起来会有ub吗


by hellomath @ 2022-06-28 10:10:28

我的建议是 std::array<std::array<int, m>, n>


by 小粉兔 @ 2022-06-28 10:18:23

如果是三维数组的话,函数签名可以是 int (*f(const int &))[1010],其中 1010 换成数组第三维大小

以及 const int (*f(const int &) const)[100]


by jijidawang @ 2022-06-28 10:20:56

@小粉兔 直接 lambda 表达式让编译器判定类型(?


by Doqe @ 2022-06-28 13:48:50

应该如果自己调用没有什么问题,接STL的话要注意const


|