为什么我不能回到列出c ++列表

时间:2015-12-30 20:23:27

标签: c++ list

我正在编写必须生成hyperCoube的程序,我必须找到最大流量值。为什么我不能将int推入我的列表"关系"在类HyperCoube中。 例如,我在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;
    }

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

1 个答案:

答案 0 :(得分:1)

当你致电AllFlowFinding时,你永远不会进入(this->adjacencyMatrix[i][startVertex] != 2)的状态,所以你永远不会进入列表。