本文整理汇总了C++中MaxHeap::push方法的典型用法代码示例。如果您正苦于以下问题:C++ MaxHeap::push方法的具体用法?C++ MaxHeap::push怎么用?C++ MaxHeap::push使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MaxHeap
的用法示例。
在下文中一共展示了MaxHeap::push方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
MinHeap<int> minHeap;
MaxHeap<int> maxHeap;
minHeap.push(10);
minHeap.push(5);
minHeap.push(12);
minHeap.push(3);
minHeap.push(4);
maxHeap.push(10);
maxHeap.push(5);
maxHeap.push(12);
maxHeap.push(3);
maxHeap.push(4);
while ( !minHeap.empty()) {
std::cout << minHeap.top() << " ";
minHeap.pop();
}
std::cout << std::endl;
while ( !maxHeap.empty()) {
std::cout << maxHeap.top() << " ";
maxHeap.pop();
}
std::cout << std::endl;
}
示例2: mexFunction
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
if( nrhs!=3 )
mexErrMsgTxt("This function requires 3 arguments\n");
if( !mxIsNumeric(prhs[0]) )
mexErrMsgTxt("parameter 1 missing!\n");
if( !mxIsNumeric(prhs[1]) )
mexErrMsgTxt("parameter 2 missing!\n");
if( !mxIsNumeric(prhs[2]) )
mexErrMsgTxt("parameter 3 missing!\n");
// retrieve the heap
MaxHeap<double>* heap;
retrieve_heap( prhs[0], heap);
// retrieve the parameters
int index;
retrieve_index( prhs[1], index );
double cost;
retrieve_cost( prhs[2], cost);
// push in the PQ
try{
heap->push( cost, index-1 );
}
catch( InvalidKeyIncreaseException exc ){
return;
}
// return control to matlab
return;
}
示例3: addNum
void addNum(int num) {
if (max_heap_.empty()) {
max_heap_.push(num);
} else {
if (num > max_heap_.top()) {
min_heap_.push(num);
} else {
max_heap_.push(num);
}
// Maintain abs(max_heap_.size() - min_heap_.size()) <= 1
if (max_heap_.size() > min_heap_.size() + 1) { // max_heap_ too large
min_heap_.push(max_heap_.top());
max_heap_.pop();
} else if (min_heap_.size() >
max_heap_.size() + 1) { // min_heap_ too large
max_heap_.push(min_heap_.top());
min_heap_.pop();
}
}
}