首页 > 其他 > 详细

STL数据结构

时间:2016-06-06 16:39:52      阅读:150      评论:0      收藏:0      [点我收藏+]
  • priority_queue

  "C++ reference"上如此解释priority queue:"This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue)."

  这里用个小例子使用下priority_queue:

#include <queue>
#include <iostream>
#include <vector>
#include <functional>

using namespace std;

class comparison{
public:
  bool operator() (const int& lhs, const int&rhs) const
  {
    return lhs < rhs;
  }
};

typedef priority_queue<int,vector<int>,comparison> mycompare;

int main(){

  vector<int> v({2,4,1,12,6,23,4});
  
  //几种申明方式
  priority_queue<int> first;
  priority_queue<int> second (myints,myints+4);
  priority_queue<int, std::vector<int>, std::greater<int> >
                            third (myints,myints+4);

  mycompare prq(v.begin(), v.end());

  while(!prq.empty()){
        cout << prq.top() << endl;
        prq.pop();
  }

  return 0;
}

 

STL数据结构

原文:http://www.cnblogs.com/JimmyTY/p/5563064.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!