[英]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.