Replies: 11 comments
-
什么也没有??? |
Beta Was this translation helpful? Give feedback.
-
想到了一个在不使用重载的 template<typename T>
struct matrix {
int row, col;
T **data;
matrix(int row, int col) : row(row), col(col), data(new T *[row]) {
*data = new T[row * col]();
for (int i = 0; i < row; i++) {
data[i] = &((*data)[i * col]);
}
}
~matrix() {
if (data != nullptr) {
delete[] *data;
delete[] data;
}
}
} 若要让 |
Beta Was this translation helpful? Give feedback.
-
一般来说,应该会使用条件编译,使越界检查只在 |
Beta Was this translation helpful? Give feedback.
-
感觉这段代码相当于为一个连续存储的二维数组的每一行首元素建立了一个索引。不过感觉要实现任意维度还是重载 说来,在 OpenCV 关于 |
Beta Was this translation helpful? Give feedback.
-
重载[]的话,需要为每个维度内建类型,才能用[][]这种连续索引,这也是为什么numpy只允许有一个[],但里面可以放元组或切片,而非允许多个[]的连续索引。由于C++没有针对元组的语法级支持,而重载()运算符可以接收任意数量的参数,因此感觉重载()并设为内联似乎更好一点?这样对位置的计算被封装在了()函数里,方便使用的同时,如果允许release版本不做越界检查,那基本也没有性能损失了。
… 在 2021年10月7日,14:19,henry_23 ***@***.***> 写道:
@kevintsq
想到了一个在不使用重载的 () 运算符的情况下,在类内部也可以用连续下标访问,但不支持 reshape 操作的维数未知的动态矩阵实现方法:
template<typename T>
struct matrix {
int row, col;
T **data;
matrix(int row, int col) : row(row), col(col), data(new T *[row]) {
*data = new T[row * col]();
for (int i = 0; i < row; i++) {
data[i] = &((*data)[i * col]);
}
}
~matrix() {
if (data != nullptr) {
delete[] *data;
delete[] data;
}
}
}
感觉这段代码相当于为一个连续存储的二维数组的每一行首元素建立了一个索引。不过感觉要实现任意维度还是重载 [] 运算符会比较方便。
说来,在 OpenCV 关于 cv::Mat 类的介绍中,比如要存储一个三维数组,就是连续存储的若干行(一维)形成一个平面(二维),若干平面(二维)再连续存储形成三维数组,按照这个规律,要访问某个元素应该也是很容易计算得到的。
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
|
Beta Was this translation helpful? Give feedback.
-
这段写错了吧,应该是:
|
Beta Was this translation helpful? Give feedback.
-
确实是写错了,感谢捉虫! |
Beta Was this translation helpful? Give feedback.
-
https://oi-wiki.org/lang/pointer/
OI Wiki 是一个编程竞赛知识整合站点,提供有趣又实用的编程竞赛知识以及其他有帮助的内容,帮助广大编程竞赛爱好者更快更深入地学习编程竞赛
Beta Was this translation helpful? Give feedback.
All reactions