簡體   English   中英

如何在 C++ 中返回結構數組

[英]How to return structure array in C++

所以我一直在嘗試實現Kruskal的算法,首先我想明確這個問題與算法的實現無關。 我創建了一個graph.hpp文件,一個kruskalsAlgo.hppmain.cpp ,分別如下:

#pragma once

struct Edge
{
    int source;
    int destination;
    int weight;
};

struct Graph
{
    int V;
    int E;

    Edge* edge;
};

Graph* create_graph(int V, int E)
{
    Graph* graph = new Graph;
    graph -> V = V;
    graph -> E = E;

    graph -> edge = new Edge[E];

    return graph;
}
#include <stdlib.h>
#include <tuple>

#include "../Graph/Graph.hpp"

class Kruskals_Algo
{
    private:
        struct subset
        {
            int parent;
            int rank;
        };

        void make_set(subset*, int);
        int find_set(subset*, int);
        void _union(subset*, int, int);
    
    public:
        Edge* kruskal(Graph*);
        void print_kruskals_MST(Edge*, int);
};

void Kruskals_Algo::make_set(subset* subsets, int V)
{
    subsets[V].parent = V;
    subsets[V].rank = 0;
}

int Kruskals_Algo::find_set(subset* subsets, int V)
{
    if(subsets[V].parent != V)
        subsets[V].parent = find_set(subsets, subsets[V].parent);
    
    return subsets[V].parent;
}

void Kruskals_Algo::_union(subset* subsets, int x, int y)
{
    int xroot = find_set(subsets, x);
    int yroot = find_set(subsets, y);

    if(subsets[xroot].rank < subsets[yroot].rank)
        subsets[xroot].parent = yroot;
    
    else if(subsets[xroot].rank > subsets[yroot].rank)
        subsets[yroot].parent = xroot;

    else
    {
        subsets[yroot].parent = xroot;
        subsets[xroot].rank++;
    }
}

inline int myComp(const void* a, const void* b)
{
    Edge* a1 = (Edge*)a;
    Edge* b1 = (Edge*)b;
    return a1 -> weight > b1 -> weight;
}

Edge* Kruskals_Algo::kruskal(Graph* graph)
{
    int V = graph -> V;
    Edge result[V];
    Edge* result_ptr = result;
    int e = 0;
    int i = 0;

    qsort(graph -> edge, graph -> E, sizeof(graph -> edge[0]), myComp);

    subset* subsets = new subset[(V * sizeof(subset))];

    for (int v = 0; v < V; ++v)
        make_set(subsets, v);

    while(e < V - 1 && i < graph -> E)
    {
        Edge next_edge = graph -> edge[i++];

        int x = find_set(subsets, next_edge.source);
        int y = find_set(subsets, next_edge.destination);

        if (x != y)
        {
            result[e++] = next_edge;
            _union(subsets, x, y);
        }
    }
    //return std::make_tuple(res, e);
    return result_ptr;
}

void Kruskals_Algo::print_kruskals_MST(Edge* r, int e)
{
    int minimumCost = 0;
    for(int i=0; i<e; ++i)
    {
        std::cout << r[i].source << " -- "
                  << r[i].destination << " == "
                  << r[i].weight << std::endl;
        minimumCost = minimumCost + r[i].weight;
    }
    
    std::cout << "Minimum Cost Spanning Tree: " << minimumCost << std::endl;
}
#include <iostream>

#include "Graph/Graph.hpp"

#include "Kruskals_Algo/kruskalsAlgo.hpp"
//#include "Prims_Algo/primsAlgo.hpp"

using namespace std;

class GreedyAlgos
{        
    public:
        void kruskals_mst();
        //void prims_mst();
};

void GreedyAlgos::kruskals_mst()
{
    Kruskals_Algo kr;
    int V;
    int E;
    int source, destination, weight;
    
    cout << "\nEnter the number of vertices: ";
    cin >> V;
    cout << "\nEnter the number of edges: ";
    cin >> E;
    
    Edge* res;

    Graph* graph = create_graph(V, E);

    for(int i=0; i<E; i++)
    {
        cout << "\nEnter source, destinstion and weight: ";
        cin >> source >> destination >> weight;
        graph -> edge[i].source = source;
        graph -> edge[i].destination = destination;
        graph -> edge[i].weight = weight;
    }

    //std::tie(result, E) = kr.kruskal(graph);
    res = kr.kruskal(graph);
    kr.print_kruskals_MST(res, E);
}

int main()
{
    int choice;
    GreedyAlgos greedy;
    greedy.kruskals_mst();
    
    return 0;
}

所以我的問題是,當我調試程序時,結構數組Edge result[V]中的值在 position [0] [1] [2]處被正確計算,如下圖所示:

在此處輸入圖像描述

但是當從主調用 function print_kruskals_MST(res, E)時,打印的值是不同的:

在此處輸入圖像描述

我做錯了什么指針的事情嗎? 提前致謝。 PS忽略評論!

這個答案可能不會直接回答你的問題,但它應該對這個問題有所了解。

首先,是的,你有很多指針問題......

其次,將new運算符的任何使用與delete運算符配對。 就目前而言,您有一堆 memory 泄漏。

另外,為什么create_graph 而是為Graph創建一個構造函數(以及一個析構函數,因為 class 有一個需要處理的Edge* edge )。

struct Graph
{
    int V;
    int E;

    Edge* edge;

    // constructor
    Graph(int V, int E)
    {
        this->V = V;
        this->E = E;
        this->edge = new Edge[E];
    }

    // destructor
    ~Graph()
    {
        // nullify the member variable before deleting its memory is just a safety measure pertaining to multithreading.
        Edge* _edge = this->edge;
        this->edge = nullptr;
        delete _edge;
    }
};

然后改變Graph* graph = create_graph(V, E); 進入Graph* graph = new Graph(V, E); 並在使用完畢后delete graph

確保刪除所有 memory 泄漏,我們可以 go 繼續討論引用正確的數據(例如,通過我更改我的答案)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM