【发布时间】:2020-01-07 16:25:18
【问题描述】:
在 C++ 中,我编写了一个包含 2D 向量的代码,并且要求用户提供 2D 向量中的输入。我觉得很难找到那个不。在 size 函数的帮助下,2 个向量中的行数。
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int diagonalDifference(vector<vector<int>> arr)
{
int d = arr[0].size();
cout << d;
return 0;
}
int main()
{
int size;
cin >> size;
vector<vector<int>> d;
for (int i = 0; i < size; i++)
{
d[i].resize(size);
for (int j = 0; j < size; j++)
{
cin >> d[i][j];
}
}
cout << diagonalDifference(d);
}
输出应该是 No. Rows ,但它是 NULL
【问题讨论】:
-
警告:将
#include <vector>与#include <bits/stdc++.h>一起使用表明您不知道#include <bits/stdc++.h>的作用。在你研究和理解之前不要使用东西。也就是说,一旦您了解了#include <bits/stdc++.h>的作用及其含义,您可能就不想使用它了。但是,如果您继续使用它,请务必小心将其与using namespace std;结合使用,因为它们更容易发生彼此的一些最坏影响。
标签: c++ vector resize 2d-vector