在c++中编写交换函数以重新分配结构数组中的成员值

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

本文关键字:分配 结构 成员 数组 新分配 c++ 交换 函数      更新时间:2023-10-16

我正在编写一个程序,该程序将使用heapsort创建一个优先级从最高到最低的队列(最小的数字是最高优先级)。

我有一个程序,可以读取任意数量的节点,首先是它的值和优先级。我添加了第三个成员location,它可以跟踪节点在数组中的位置。每次我向数组中添加一个节点时,我都会增加堆的大小(从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函数将识别哪个优先级最小,交换函数将交换每个节点的值和优先级,但当我调用交换函数时,它似乎对heapifyop函数中的节点没有任何影响。

您的问题是,在Swap函数中,您按值获取参数,返回一个新参数,而不将其分配给任何对象。表达式return(a,b);并不像你想象的那样:C++中默认的逗号运算符计算其第一个参数,然后返回第二个参数,因此return(a,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