gpt4 book ai didi

c++ - 为什么我不能推回列表 C++

转载 作者:行者123 更新时间:2023-11-28 05:54:22 24 4
gpt4 key购买 nike

我正在编写必须生成 hyperCoube 的程序,我必须找到最大流量值。为什么我不能在 HyperCoube 类中将 int 插入我的列表“关系”。例如,我在 HyperCoube.AllFlowFinding 方法中推送 int。

    #include <iostream>
#include <list>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <climits>
#include <iomanip>
#include <string>
#include <list>

using namespace std;

class HyperCoube {
int vertex;
int traces;
double ** adjacencyMatrix;
list<int> relations;
public:

HyperCoube(int k) {
this->traces = 0;
this->vertex = (int) pow(2.0, (double) k);
CreateDefaultAdjacencyMatrix();
CreateAdjacencyMatrix();
}

void PrintMatrix() {
for (int i = 0; i<this->vertex; i++) {
for (int j = 0; j<this->vertex; j++) {
cout << this->adjacencyMatrix[i][j] << " ";
}
cout << endl;
}
}

void PrintDagonalMatrix() {
for (int i = 0; i<this->vertex; i++) {
for (int j = 0; j<this->vertex; j++) {
if (this->adjacencyMatrix[i][j] == 2) {
cout << 0 << " ";
} else {
cout << 1 << " ";
}
}
cout << endl;
}
}

void printQuantityTraces() {
cout << this->traces << endl;
}

void AllPathFinding(int startVertex, int exitVertex) {
for (int i = 0; i<this->vertex; i++) {
if (exitVertex != i) {
if (this->adjacencyMatrix[i][startVertex] != 2) {
cout << startVertex << " " << i << endl;

AllPathFinding(i, exitVertex);
}
} else {
if (this->adjacencyMatrix[i][startVertex] != 2) {
this->traces++;
cout << startVertex << "+++" << i << endl;
break;
}
}

}
}

void PrintList() {
list<int>::iterator iter;
for (iter = this->relations.begin(); iter != this->relations.end(); ++iter) {
int rel = *iter;
cout << rel<< "; ";
}
}

/* ~HyperCoube() {
for (int i = 0; i<this->vertex; i++)
delete [] this->adjacencyMatrix[i];
delete [] * this->adjacencyMatrix;
}*/
private:

void AllFlowFinding(int startVertex, int exitVertex) {
for (int i = 0; i<this->vertex; i++) {
if (exitVertex != i) {
if (this->adjacencyMatrix[i][startVertex] != 2) {
cout << startVertex << " " << i << endl;
this->relations.push_back(startVertex);
this->relations.push_back(i);
AllPathFinding(i, exitVertex);
}
} else {
if (this->adjacencyMatrix[i][startVertex] != 2) {
this->traces++;
cout << startVertex << "+++" << i << endl;
this->relations.push_back(startVertex);
this->relations.push_back(i);
break;
}
}

}
}

void CreateDefaultAdjacencyMatrix() {
this->adjacencyMatrix = new double *[this->vertex];
for (int i = 0; i<this->vertex; i++) {
this->adjacencyMatrix[i] = new double [this->vertex];
}
for (int i = 0; i<this->vertex; i++) {
for (int j = 0; j<this->vertex; j++) {
this->adjacencyMatrix[i][j] = 2.0;
}
}
}

void CreateAdjacencyMatrix() {
for (int i = 0; i < this->vertex; i++) {
for (int j = 0; j < i; j++) {
int u = i ^ j;
int k = u - 1;
k |= k >> 1;
k |= k >> 2;
k |= k >> 4;
k |= k >> 8;
k |= k >> 16;
if (k + 1 == u) {
this->adjacencyMatrix[i][j] = rand() / (double) RAND_MAX;
}
}
}
}
};

int main() {
srand(time(NULL));
HyperCoube x(4);
x.PrintDagonalMatrix();
x.AllPathFinding(0, 2);
//x.printQuantityTraces();
x.PrintList();
return 0;
}

当我打印列表大小时,它总是打印零。

最佳答案

当你调用 AllFlowFinding 时,你永远不会进入 (this->adjacency Matrix[i][start Vertex] != 2) 的条件,所以你是从未真正插入列表。

关于c++ - 为什么我不能推回列表 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34536709/

24 4 0