创建大小为其他两个矩阵大小的矩阵时超出范围
Out of range when creating a matrix whose sizes are sizes of two other matrices
假设我有两个矩阵,称为 A 和 B,我想创建一个新的矩阵 C,其中 C 的行数等于矩阵 A 和 B 的行数的乘积。C 的列数也等于 A 和 B 的列数的乘积。
当任何矩阵 A 或 B 是空矩阵时,我的程序不起作用。
我尝试将矩阵 A 和 B 的大小设置为与零不同的值,该程序有效。
// this function "creates" and returns a matrix of the size mentioned in parameters
vector<vector<int>> CreateMatrix (int number_of_rows, int number_of_columns) {
return vector<vector<int>>(number_of_rows, vector<int> (number_of_columns));
}
int main()
{
vector<vector<int>> a;
vector<vector<int>> b;
int no_rows = a.size() * b.size(); // multiplies no. of rows A and B
int no_cols = a.at(0).size() * b.at(0).size(); // multiplies number of columns of A and B
auto c = CreateMatrix(no_rows, no_cols).
cout << c.size();
我预计程序会打印"0",因为这将是矩阵 C 的行数,但它不会崩溃。
vector<vector<int>> a;
vector<vector<int>> b;
int no_cols = a.at(0).size() * b.at(0).size(); // multiplies number of columns of A and B
在这里,您可以访问两个空向量的第一个元素,因此自然会崩溃。
相关文章:
- 如何在c/c++中检查两个任意内存范围是否重叠
- 我应该如何使用remove_if删除两个数字范围内的元素
- 同一范围内矢量的两个独立"for"循环,仅打印第一个循环
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 是否存在用于按以下方式对两个范围进行排序和分区的标准算法?
- 如何连接两个现有范围::视图
- 创建大小为其他两个矩阵大小的矩阵时超出范围
- 范围V3.两个范围之间的区别
- 为什么基于指针交换两个值在函数范围之外不起作用?
- 是否有使用谓词比较两个范围的标准方法
- 为什么需要两个循环范围来更改C++向量的这些元素
- <algorithm>在同一输入迭代器范围内并排运行两个
- 重载函数的调用是不明确的(两个函数在不同的范围内)
- C++ - 查找两个范围的交集
- 如何让代码存在于两个或多个非嵌套命名空间的范围内
- 在C++中以相同的方式同时对两个元素范围进行分区
- 按元素添加两个范围的惯用和有效方法
- 如何用两个给定的整数做一个较小的范围
- 如何检查两个数字是否在浮点类型的精度限制的有效数字"x"范围内?
- 将两个范围的值相加并将它们放入第三个范围的算法