我正在将一些函数从Matlab转换为C++,并且与矩阵有关.我在互联网上的某处发现了这个简单的功能:
typedef std::vector<std::vector<double> > Matrix;
Matrix sum(const Matrix& a, const Matrix& b) {
size_t nrows = a.size();
size_t ncols = a[0].size();
Matrix c(nrows, std::vector<double>(ncols));
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么他们用作const Matrix& a输入,而不是Matrix a?他们是否习惯使用它,或使用它有什么好处,因为我没有看到2个版本的结果(const Matrix& a和Matrix a输入)之间有任何差异.
我知道这个问题听起来很荒谬,但我需要专家的确认,所以请让我解释一下情况:
我正在调试一个C++代码(很长,~5000行),我发现了一些奇怪的东西,我试图简化如下:
myClass.h
class myclass
{
...
void myfun(int p1, int p2, mytype *p3, bool isFirstTime);
...
}
=================================================================
myClass.cpp
...
void myclass::myfun(int p1, int p2, mytype *p3, bool isFirstTime)
{
...
if (mycond[y] == false)
{
myarr[y] = p1;
myfun(y, y, p3); <--- here no bool parameter given (*)
}
...
}
...
Run Code Online (Sandbox Code Playgroud)
代码可以编译和运行,没有任何错误或警告(用于功能myfun).但由于if-else代码中有许多内容,我不确定(*)在进程中是否实际调用了该行的命令.
所以问题是:这条线是否使用正确?如果正确,请解释我或给我一些关于这种"类型"功能的信息.如果不正确,为什么编译时没有警告或错误?
c++ ×2