我想将2D数组传递给另一个函数。我有一个代码的例子,我在main函数中都有。但是,赋值要求我们将代码分成总共三个函数,main,adjmatrix和adjlist函数。
所有主要功能
#include<iostream>
#include<fstream>
using namespace std;
int main(void)
{
ifstream in;
char infile[40];
int c, u, v;
cout << "Please enter the input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
while(in.fail()) {
cout << "Please enter a CORRECT input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
}
//adj matrix
cout << "Adjacency Matrix" << endl;
in >> c;
int array[c][c];
for(int i=0; i<c; i++) {
for(int j=0; j<c; j++) {
array[i][j] = 0;
}
}
while(in >> u >> v) {
array[u][v] = 1;
}
cout << c << endl;
for(int i=0;i<c;i++) {
cout << i << " ";
for(int j=0;j<c;j++){
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
//adj list
cout << "Adjacency List" << endl;
cout << c << endl;
for(int i=0;i<c;i++) {
cout << i << " --> ";
for(int j=0;j<c;j++) {
if(array[i][j] == 1) {
cout << j << " ";
}
}
cout << endl;
}
in.close();
return 0;
}
该程序从以下输入文件
输出邻接矩阵和邻接列表9
2 8
0 6
8 5
2 4
3 1
2 3
4 1
6 1
2 6
7 5
1 7
以下是输出
Adjacency Matrix
9
0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 1 0
2 0 0 0 1 1 0 1 0 1
3 0 1 0 0 0 0 0 0 0
4 0 1 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 1 0 0 0 0 0 0 0
7 0 0 0 0 0 1 0 0 0
8 0 0 0 0 0 1 0 0 0
Adjacency List
9
0 --> 6
1 --> 7
2 --> 3 4 6 8
3 --> 1
4 --> 1
5 -->
6 --> 1
7 --> 5
8 --> 5
我读过传递2D数组的地方需要输入第二个维度。我还读过一些关于它必须是全局常数的东西?所以我可能有点疯狂,然后在尝试一些事情的疯狂道路上走开,所以请原谅一些愚蠢。我认为我遇到的问题是实际的数组大小来自文件所以我真的不明白在 int c 初始化时用什么值来声明数组的第二个维度和实际值到输入文件中的第一个值。以下是我尝试传递2D数组失败的原因。享受:
#include<iostream>
#include<fstream>
using namespace std;
const int c;
void adjmatrix(istream &in, int array[][c]);
int main(void)
{
ifstream in;
char infile[40];
cout << "Please enter the input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
while(in.fail()) {
cout << "Please enter a CORRECT input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
}
in >> c;
int array[c][c];
adjmatrix(in, array);
in.close();
return 0;
}
void adjmatrix(istream &in, int array[][c])
{
int u,v;
for(int i=0; i<c; i++) {
for(int j=0; j<c; j++) {
array[i][j] = 0;
}
}
while(in >> u >> v) {
array[u][v] = 1;
}
cout << c << endl;
for(int i=0; i<c; i++) {
cout << i << " ";
for(int j=0; j<c; j++) {
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
答案 0 :(得分:0)
您可以动态分配数组,而不是静态定义它。
int i;
int **array;
in >> c;
array = new int*[c];
for(i=0;i<c;i++)
array[i] = new int[c];
adjmatrix(in, array,c);
函数adjmatrix的声明为adjmatrix(istream &in, int **array,int c);
答案 1 :(得分:0)
以下可能会有所帮助:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
void print_adjacency_matrix(const std::vector<std::vector<int>>& mat)
{
std::cout << "Adjacency Matrix" << std::endl;
std::cout << mat.size() << std::endl;
for(std::size_t i = 0; i != mat.size(); ++i) {
std::cout << i << " ";
for(auto e : mat[i]) {
std::cout << e << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void print_adjacency_list(const std::vector<std::vector<int>>& mat)
{
std::cout << "Adjacency List" << std::endl;
std::cout << mat.size() << std::endl;
for (std::size_t i = 0; i != mat.size(); ++i) {
std::cout << i << " --> ";
for(auto e : mat[i]) {
if (e == 1) {
std::cout << e << " ";
}
}
std::cout << std::endl;
}
}
int main()
{
std::ifstream in;
std::string infile;
std::cout << "Please enter the input data file name(NO SPACES): ";
std::cin >> infile;
in.open(infile);
while(in.fail()) {
std::cout << "Please enter a CORRECT input data file name(NO SPACES): ";
std::cin >> infile;
in.open(infile);
}
int c;
in >> c;
std::vector<std::vector<int> > array(c, std::vector<int>(c));
int u, v;
while(in >> u >> v) {
array[u][v] = 1;
}
in.close();
print_adjacency_matrix(array);
print_adjacency_list(array);
return 0;
}