簡體   English   中英

編寫交換函數以在C ++中的結構數組中重新分配成員值

[英]Writing swap function to reassign member values in an array of structures in c++

我正在編寫一個程序,該程序將使用堆排序創建從最高優先級到最低優先級(最小的數字即最高優先級)的優先級隊列。

我有一個程序,可以讀取任意數量的節點,首先是它的值和優先級。 我添加了第三個成員,位置,它可以跟蹤節點在陣列中的位置。 每次向數組添加節點時,都會增加堆的大小(從0開始)。 由於將節點添加到數組的末尾,因此其初始位置等於堆的大小。

執行排序時,我將每個節點的優先級與其子節點進行比較。 如果子節點的優先級低於其父節點的優先級,則我編寫的交換函數應交換兩個節點的優先級以及兩個節點的值。 我在下面附加了構建堆,堆化和交換功能:

    void Heap::buildheap(heaptype *A, heaptype node)
    {
            for(int i = heapsize; i>0; i--)
            {
                    heapifyup(A,A[i-heapsize]);
            }
    }      

    void Heap::heapifyup(heaptype *A, heaptype node)
    {
            heaptype min;
            heaptype r = getright(A, node);
            heaptype l = getleft(A, node);

            if(l.location<=heapsize && l.k<node.k)
            {
                    min = l;
            }

            else
            {
                    min = node;
            }

            if(r.location<=heapsize && r.k<min.k)
            {
                    min = r;
            }

            if(min.k != node.k)
            {
                    Swap(min,node);
                    heapifyup(A, min);
            }

    }

    heaptype Heap::Swap (struct heaptype a,struct heaptype b)
    {
            heaptype temp;
            temp.id = a.id;
            a.id = b.id;
            b.id = temp.id;
            temp.k = a.k;
            a.k = b.k;
            b.k = temp.k;

        return(a, b);
    }

這是顯示我標記的相同代碼:

    void Heap::heapifyup(heaptype *A, heaptype node)
    {
    ...
            if(min.k != node.k)
            {
                    cout<<"1Minimum id: "<<min.id<<endl;
                    cout<<"1Minimum priority: "<<min.k<<endl;
                    cout<<"1Minimum location: "<<min.location<<endl;
                    cout<<"1node id: "<<node.id<<endl;
                    cout<<"1node priority: "<<node.k<<endl;
                    cout<<"1node location: "<<node.location<<endl;

                    Swap(min,node);

                    cout<<"final Minimum id: "<<min.id<<endl;
                    cout<<"final Minimum priority: "<<min.k<<endl;
                    cout<<"final Minimum location: "<<min.location<<endl;
                    cout<<"final node id: "<<node.id<<endl;
                    cout<<"final node priority: "<<node.k<<endl;
                    cout<<"final node location: "<<node.location<<endl;

                    heapifyup(A, min);

            }

    }

    heaptype Heap::Swap (struct heaptype a,struct heaptype b)
    {
            cout<<"intial a id: "<<a.id<<endl;
            cout<<"initial a priority: "<<a.k<<endl;
            cout<<"initial a location: "<<a.location<<endl;
            cout<<"initial b id: "<<b.id<<endl;
            cout<<"initial b priority: "<<b.k<<endl;
            cout<<"initial b location: "<<b.location<<endl;

            heaptype temp;
            temp.id = a.id;
            a.id = b.id;
            b.id = temp.id;
            temp.k = a.k;
            a.k = b.k;
            b.k = temp.k;

            cout<<"final a id: "<<a.id<<endl;
            cout<<"final a priority: "<<a.k<<endl;
            cout<<"final a location: "<<a.location<<endl;
            cout<<"final b id: "<<b.id<<endl;
            cout<<"final b priority: "<<b.k<<endl;
            cout<<"final b location: "<<b.location<<endl;

        return(a, b);
    }

這表明使用輸入(2 3,3 2),heapifyup函數將確定哪個優先級最小,並且swap函數將交換每個節點的值和優先級,但是當我調用swap函數時,它似乎沒有對heapifyup函數中的節點的任何影響。

您的問題是,在Swap函數中,您Swap值獲取參數,返回一個新參數,而不是將其分配給任何東西。 表達式return(a,b); 並沒有執行您認為的操作:C ++中的默認逗號運算符計算其第一個參數,然后返回第二個參數,因此return(a,b); return b;相同return b; 您可以返回一個std::pair<heaptype,heaptype>並在調用站點上分配內容,但是最好的選擇是通過引用獲取參數:

heaptype Swap (struct heaptype a,struct heaptype b);
void Swap (heaptype& a, heaptype& b); //change to this

您有兩個問題:第一個問題是,當您按值將參數傳遞給函數時,將復制值,並且更改函數內部的值將僅修改副本,而不修改原始值。 C ++允許您按引用傳遞這意味着參數是原始文檔的引用,並且在函數內部更改參數也會修改原始文檔(因為它們都相同)。

您遇到的第二個問題是您的退貨單。 您不能從一個函數返回多個值,實際上您使用的是逗號表達式,該表達式在逗號的兩邊都計算兩個子表達式,但會抵消右邊的值。 因此, return a,b本質上等於return b

根據您要執行的操作,您可以通過引用傳遞參數,例如

void Swap (struct heaptype& a, struct heaptype& b)

(請注意,“&”號)

或者,如果您要按值傳遞並返回兩個值,則可以返回std::pair

std::pair<heaptype, heaptype> Swap (struct heaptype a, struct heaptype b)
{
    ...

    return std::make_pair(a, b);
}

如果要交換整個結構,則可以使用std::swap代替。

暫無
暫無

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

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