码迷,mamicode.com
首页 > 其他好文 > 详细

STL数据结构

时间:2016-06-06 16:42:50      阅读:169      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!