二维动态数组 C++显示问题
我读过有关二维动态数组的内容,但显然我还没有完全理解它,因为这个程序不起作用。该程序似乎在于显示数组。 输入文件是一个文本文件,第一行有 V 和 E,它们之间有“制表符缩进”。输入顶点再次位于下一行,每行缩进一个新的集合。在 DevCpp 上它说存在分段错误。任何帮助将非常感激。谢谢。
#include <iostream>
#include <fstream>
using namespace std;
#define maxV 100
#define unseen 0
typedef int Vertex;
class Graph {
private:
int V, E;
int**adj;
public:
Graph(char filename[]);
void display();
};
// constructor ask you for file name
Graph::Graph(char fname[]) {
Vertex u,v;
int j;
ifstream f;
f.open(fname, ios::in);
if(!f) {
cout << "\nError: Cannot open file\n";
return;
}
//Input number of vertices and edges
f >> V >> E;
int** adj = new int*[V];
for (int i=0;i<=V;i++)
{
adj[i]= new int[V];
}
for(int x=0;x<=V; ++x) // initially 0 array
{
for (int y=0;y<=V;++y)
adj[x][y] = 0;
}
// Set diagonal to 1
for(int z=0; z<=V; ++z)
adj[z][z]=1;
for (j =0;j<=E;++j)
{
f>>u>>v;
adj[u][v] = 1;
adj[v][u] = 1;
}
}
// This method displays the adjacency lists representation.
void Graph::display(){
int a,b,c;
for (a=0;a<=V;++a)
{
cout << a << " ";
}
cout << endl;
for (b=0;b<=V;++b)
{
cout << b << "| ";
for (c=0;c<=V;++c)
{
cout<<adj[b][c]<<"| ";
}
cout<<endl;
}
}
int main()
{
char fname[20];
cout << "\nInput name of file with graph definition: ";
cin >> fname;
Graph g(fname);
g.display();
}
I've read about 2d dynamic arrays but I obviously haven't quite got my head around it as this program doesn't work. The program seems to lie in displaying the array.
The input file is a text file with V and E on the first line with a 'tab indent' between them. The input vertices are on the next lines again tab indented with a new set on each line. On DevCpp it says there is a segmentation fault. Any help would be very much appreciated. thanks.
#include <iostream>
#include <fstream>
using namespace std;
#define maxV 100
#define unseen 0
typedef int Vertex;
class Graph {
private:
int V, E;
int**adj;
public:
Graph(char filename[]);
void display();
};
// constructor ask you for file name
Graph::Graph(char fname[]) {
Vertex u,v;
int j;
ifstream f;
f.open(fname, ios::in);
if(!f) {
cout << "\nError: Cannot open file\n";
return;
}
//Input number of vertices and edges
f >> V >> E;
int** adj = new int*[V];
for (int i=0;i<=V;i++)
{
adj[i]= new int[V];
}
for(int x=0;x<=V; ++x) // initially 0 array
{
for (int y=0;y<=V;++y)
adj[x][y] = 0;
}
// Set diagonal to 1
for(int z=0; z<=V; ++z)
adj[z][z]=1;
for (j =0;j<=E;++j)
{
f>>u>>v;
adj[u][v] = 1;
adj[v][u] = 1;
}
}
// This method displays the adjacency lists representation.
void Graph::display(){
int a,b,c;
for (a=0;a<=V;++a)
{
cout << a << " ";
}
cout << endl;
for (b=0;b<=V;++b)
{
cout << b << "| ";
for (c=0;c<=V;++c)
{
cout<<adj[b][c]<<"| ";
}
cout<<endl;
}
}
int main()
{
char fname[20];
cout << "\nInput name of file with graph definition: ";
cin >> fname;
Graph g(fname);
g.display();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在初始化数据数组的代码中看到两个重要问题。首先,像这样的循环
会循环遍历比数组中实际存在的元素多的元素。如果数组的长度是 V 个元素,则循环的正确形式是
“小于”而不是“小于或等于”。
其次,将指针数组分配为 V 指针长,并将各个列分配为 V 元素长;但稍后您使用相同的数组并期望其大小为 V x E。总而言之,我认为分配代码应该是
其他地方可能存在其他错误,但至少我已经让您开始了。
I see two significant problems just in the code that initializes the data array. First, a loop like this
loops over one more element than actually exists in the array. The correct form of a loop if the array is V elements long is
That's "less than" rather than "less than or equal".
Secondly, you allocate both the array of pointers to be V pointers long, and than the individual columns to be V elements long as well; but later you use the same array and expect it to be V x E in size. Altogether, then, I think the allocation code ought to be
There are likely to be other errors elsewhere, but at least I've got you started.
我不知道哪一行导致分段错误,但这里有一些需要注意的事项:
u
和v
是否保证小于V?如果不是,你可能会写在矩阵的边界之外。
当
j == E
时会发生什么?您正在尝试读取文件中最后一行之后的一行。您应该检查j < E。更好的方法仍然是一起忽略
E
并执行以下操作:尽管分段错误更有可能在这里:
for 循环条件应该检查
b
b
b
b
b
b
b
b
V
和c < V
不是<=
。当b
或c == V
时,你肯定是在矩阵之外读取。I don't know which line is causing the segmentation fault but here are some things to look at:
Are
u
andv
guaranteed to be less thanV
? If not you could be writing outside the bounds of the matrix.What happens when
j == E
? You are trying to read a line past the last line in the file. You should be checking instead forj < E
. A better way still would be to ignoreE
all together and just do this:More likely though the segmentation fault is here:
the for loop conditionals should be checking
b < V
andc < V
not<=
. when eitherb
orc == V
you are definitely reading outside the matrix.