当前位置: 首页>>代码示例>>C++>>正文


C++ MaxHeap::top方法代码示例

本文整理汇总了C++中MaxHeap::top方法的典型用法代码示例。如果您正苦于以下问题:C++ MaxHeap::top方法的具体用法?C++ MaxHeap::top怎么用?C++ MaxHeap::top使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MaxHeap的用法示例。


在下文中一共展示了MaxHeap::top方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
开发者ID:faterer,项目名称:groof_off,代码行数:29,代码来源:MaxMinHeap_v1.cpp

示例2: findMedian

 double findMedian() {
   /*
   Three cases: since abs(max_heap_.size() - min_heap_.size()) <= 1
   denote x as min(max_heap_.size() - min_heap_.size())
   1) size x and x means even elements so it just the average of max of first
   heap and min of second heap 2) size x + 1 and x means odd elements so the
   max of the first heap is the median element 3) size x and x + 1 means odd
   elements so the min of the second heap is the median element
   */
   if (max_heap_.size() == min_heap_.size()) {
     return (double)(max_heap_.top() + min_heap_.top()) / 2.0;
   } else if (max_heap_.size() > min_heap_.size()) {
     return max_heap_.top();
   } else {
     return min_heap_.top();
   }
 }
开发者ID:jamarshon,项目名称:leetcode,代码行数:17,代码来源:295FindMedianfromDataStream.cpp

示例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();
      }
    }
  }
开发者ID:jamarshon,项目名称:leetcode,代码行数:21,代码来源:295FindMedianfromDataStream.cpp

示例4: printLocalMax

void printLocalMax(const int *arr, size_t len, size_t window) {
  MaxHeap<ValueWrapper> h;
  for (int i = 0; i < window - 1; ++i) {
    h.add(ValueWrapper(arr[i], i));
  }
  for (int i = window - 1; i < len; ++i) {
    h.add(ValueWrapper(arr[i], i));
    for(;;) {
      ValueWrapper *r = h.top();
      if (i - r->pos < window) {
	cout << r->value << endl;
	break;
      }
      h.pop();
    }
  }
}
开发者ID:jiaz,项目名称:algorithms,代码行数:17,代码来源:local_max.cpp

示例5: mexFunction

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
	if( nrhs!=1 )
		mexErrMsgTxt("This function requires 3 arguments\n");
	if( !mxIsNumeric(prhs[0]) )
		mexErrMsgTxt("parameter 1 missing!\n");

	// retrieve the heap
	MaxHeap<double>*  heap;
	retrieve_heap( prhs[0], heap);


	// extract head before popping
	pair<double, int> curr = heap->top();
	plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
	*mxGetPr(plhs[0]) = curr.second+1;
	plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
	*mxGetPr(plhs[1]) = curr.first;

	// pop top element in the PQ
	heap->pop();
}
开发者ID:Elucidation,项目名称:UAV-Motion-Planner-Ensemble,代码行数:21,代码来源:pq_pop.cpp


注:本文中的MaxHeap::top方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。