英文:
Returning different datatype depending on the need
问题
我目前正在实现自己的矩阵数据结构,其中一个我遇到困难的代码块如下:
T& operator()(size_t x){
if(rows == 1){
if(x >= cols){
throw runtime_error("Akses indeks di luar batas.");
} else return mat[0][x];
} else {
matrix<T> result(1, cols);
for(int i = 0; i < cols; i++){
result(1, i) = mat[x][i];
}
return result;
}
}
这看起来确实有问题,因为在 else 分支中试图返回 `matrix<T>`,但函数声明返回类型是 `T`。
我尝试实现的目标是,当访问 `matrix1(i)` 时,如果 `matrix1` 是一维的,即 `matrix1<int> = {1, 2, 3, 4}`,它将返回一个标量,而如果 `matrix2` 是二维的,即 `matrix2<int> = {{1, 2, 3}, {4, 5, 6}}`,访问 `matrix2(0)` 将返回一个一维矩阵(或数学向量),在这种情况下是整个第一行 `{1, 2, 3}`。
例如,如果我想定义一个新的矩阵 `mat`,它只是 `matrix1` 的一行,我可以这样做:`matrix<int> mat = matrix[1]`。
此外,我想要一种功能,当我将 `matrix[1]` 作为函数的参数时,它将传递特定的第一行。
这种功能可能类似于 numpy 数组。
非常感谢。
我已经陷入了一段时间,没有解决方案。
英文:
I am currently implementing my own matrix data structure, one particular block of code that I have trouble dealing with is the following:
T& operator()(size_t x){
if(rows == 1){
if(x >= cols){
throw runtime_error("Akses indeks di luar batas.");
} else return mat[0][x];
} else {
matrix<T> result(1, cols);
for(int i = 0; i < cols; i++){
result(1, i) = mat[x][i];
}
return result;
}
}
This indeed looks wrong since the function returns T
while in the else condition tries to return a matrix<T>
.
The goal that I am trying to achieve is that when accesing matrix1(i)
while matrix1
is one dimensional i.e matrix1<int> = {1, 2, 3, 4}
it will return a scalar while if matrix2
is a 2 dimensional i.e matrix2<int> = {{1, 2, 3}, {4, 5, 6}}
accesing matrix2(0)
will return a 1 dimensional matrix (or a mathematical vector) that in this case is the entire first row {1, 2, 3}
.
For example if I want to define a new matrix mat
that is just one row of matrix1, I can just do matrix<int> mat = matrix[1]
for example.
Also I want to have a functionality such that when I let matrix[1]
as a parameter in a function it will pass just the row 1 in particular.
This functionality is perhaps similar to numpy array.
Thank you in advanced.
I have been stuck for quite a while with no solution.
答案1
得分: 4
你可以使用 std::variant
:
std::variant<T, matrix<T>> operator()(size_t x) {
if (rows == 1) {
//...
return std::variant<T, matrix<T>>(mat[0][x]);
}
else {
matrix<T> result(1, cols);
//...
return std::variant<T, matrix<T>>(result);
}
}
你可以使用 std::holds_alternative
检查结果是否包含特定值,使用 std::get
访问元素:
std::variant<T, matrix<T>> element = m(0); // 访问第一个元素
if (std::holds_alternative<T>(element))
{
// 返回的元素是标量
T e = std::get<T>(element); // 假定类型 T 有复制构造函数
//...
}
else
{
// 返回的元素是矩阵
matrix<T> e = std::get<matrix<T>>(element); // 假定类型 'matrix' 有复制构造函数
//...
}
英文:
You can use std::variant
:
std::variant<T, matrix<T>> operator()(size_t x) {
if (rows == 1) {
//...
return std::variant<T, matrix<T>>(mat[0][x]);
}
else {
matrix<T> result(1, cols);
//...
return std::variant<T, matrix<T>>(result);
}
}
You can check if the result contains a particular value using std::holds_alternative
, and you can access the element using std::get
:
std::variant<T, matrix<T>> element = m(0); //access first element
if (std::holds_alternative<T>(element))
{
//element returned is a scalar
T e = std::get<T>(element); //Assumes the type T has a copy constructor
//...
}
else
{
//element returned is a matrix
matrix<T> e = std::get<matrix<T>>(element); //Assumes the type 'matrix' has a copy constructor
//...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论