我的矩阵乘法函数模板如下:
template<typename element_type>
void matMul(const std::vector<std::vector<element_type>> & mat1,
const std::vector<std::vector<element_type>> & mat2,
std::vector<std::vector<element_type>> & result
) {
if (mat1[0].size() != mat2.size()) {
std::cout << "dimensions do not match..." << std::endl;
return;
}
result.resize(mat1.size());
for (std::vector<double> & row : result) {
row.resize(mat2[0].size());
}
for (unsigned int row_id = 0; row_id < mat1.size(); ++row_id) {
for (unsigned int col_id = 0; col_id < mat2[0].size() < col_id; ++col_id) {
for (unsigned int element_id = 0; element_id < mat1[0].size(); ++element_id) {
////////////////////////////////////////////////////////////////////////////////////
result[row_id][col_id] += mat1[row_id][element_id] * mat2[element_id][col_id];//HERE I WILL MENTION BELOW...
////////////////////////////////////////////////////////////////////////////////////
}
}
}
我通过了
std::vector<std::vector<double>> mul1 = {
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0}
};
,
std::vector<std::vector<double>> mul2 = {
{7.0, 8.0},
{9.0, 10.0},
{11.0, 12.0}
};
和
std::vector<std::vector<double>> result;
下一个代码是测试:
matMul(mul1, mul2, result);
for (std::vector<double> row : result) {
for (double element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
输出结果为:
0 0
0 0
当我尝试在Visual Studio 2017中进行调试时,我发现断点在我上面提到的地方不起作用。它似乎什么也没做,只是为了通过这部分。为什么我的VS2017忽略了这部分?以及如何解决它?
答案 0 :(得分:3)
for (unsigned int col_id = 0; col_id < mat2[0].size() < col_id; ++col_id) {
检查终止条件。这似乎不对。你的意思是:
for (unsigned int col_id = 0; col_id < mat2[0].size(); ++col_id) {