我在使用new时遇到了一些问题。我希望你能指出我做错了什么。这是我的代码示例:
unsigned ** Create_Matrix(const unsigned &n) {
unsigned **matrix=new unsigned*[n];
for (unsigned i = 0; i < n; ++i)
matrix[i]=new unsigned[n];
return matrix;
}
int main() {
unsigned n;
std::cin>>n;
unsigned** matrix=Create_Matrix(n);
return 0;
}
试着忘记你曾经知道new
和new[]
。真。我是认真的。
#include <vector>
#include <iostream>
using value_type = double; // Or whatever
using row = std::vector<value_type>;
using matrix = std::vector<row> ;
matrix create_matrix(unsigned n) {
matrix mat(n);
for (auto& r : mat) {
r.resize(n);
}
return mat;
}
int main() {
unsigned n;
std::cin >> n;
auto mat = create_matrix(n);
return 0;
}