c++ for循环中数值奇大
创建图的时候发生异常for循环中j的数值非常大
图的邻接矩阵
源码:
#include <iostream>
using namespace std;
template<typename V,typename E>
class adjMatrix {
private:
V* vertexs;
E** edges;
E noEdge;
//int verNum, edgeNum;
//bool* visited;
void dfs(int start)const;
public:
adjMatrix(int size, E noEdgeFlag) {
verNum = size;
edgeNum = 0;
noEdge = noEdgeFlag;
vertexs = new V;
edges = new E * ;
for (int i = 0; i < verNum; i++) {
edges = new E;
for (int j = 0; j < verNum; ++j)
edges = noEdge;
}
visited = new bool;
TE = new mstEdge;
}
~adjMatrix() {
delete[] vertexs;
for (int i = 0; i < verNum; i++)
delete[] edges;
delete[] edges;
delete[] visited;
delete[] TE;
}
void createGraph(const V V[], const E E[]);
void printGraph()const;
//bool searchEdge(int from, int to)const;
bool insertEdge(int from, int to, E w);
//bool removeEdge(int from, int to);
void dfsTraverse()const;
void bfsTraverse()const;
protected:
struct mstEdge {
int vex1, vex2;
E weight;
bool operator<(const mstEdge& e)const {
return weight < e.weight;
}
}*TE;
int verNum, edgeNum;
bool* visited;
};
template<typename V, typename E>
void adjMatrix<V, E>::dfs(int start)const {
int i;
cout << vertexs<< ' ';
visited = true;
for (i = 0; i < verNum; i++) {
if (edges != noEdge && visited == false)
dfs(i);
}
}
template<typename V, typename E>
bool adjMatrix<V, E>::insertEdge(int from, int to, E w) {
if (from<0 || from>verNum - 1 || to<0 || to>verNum - 1)
return false;
if (edges == w)
return false;
if (edges == noEdge)
++edgeNum;
edges = w;
return true;
}
template<typename V, typename E>
void adjMatrix<V, E>::createGraph(const V Vv[], const E Ee[]) {
int i, j;
for (i = 0; i < verNum; i++)
vertexs = Vv;
for (i = 0; i < verNum; i++) {
for (j = 0; i < verNum; j++)
if (Ee > 0)
insertEdge(i, j, Ee);
}
}
int main()
{
int a = {1,2,3};
int b = {0,1,1,0,0,1,0,1,0};
adjMatrix<int,int>matrix(3,0);
matrix.createGraph(a, b);
return 0;
} 发现问题了第二个for循环应该是j<verNum{:10_250:} {:10_245:}第二个for循环应该是j<verNum
页:
[1]