在 C++ 中,当添加到调用 bubbleUp 函数的 minHeap 时,如何按字典顺序比较两个具有相同优先级的事物?
我希望按字典顺序比较时较小的值在堆中排在第一位。if 条件应该是什么?
如果代码是这样的:
void MinHeap::bubbleUp(int pos)
{
if (pos >= 0 && vec[pos].second < vec[(pos-1)/d].second)]
{
swap(vec[pos], vec[(pos-1)/d)];
bubbleUp(vec[(pos-1)/d)];
}
else if (pos >= 0 && vec[pos].second == vec[(pos-1)/d].second)
{
if(vec[pos].first < vec[(pos-1)/d].first)
{
swap(vec[pos], vec[(pos-1)/d];
bubbleup((pos-1)/d];
}
}
}
作为参考,向量包含一对字符串和优先级。