zoukankan      html  css  js  c++  java
  • STL Sample[make_heap]

    #include <iostream>
    #include <algorithm>
    #include <deque>
    using namespace std;
    
    struct Node 
    {
        int x,y;
        Node(int a = 0,int b = 0):x(a),y(b){}
        	
        bool operator <(const Node& n)
        {
            if (x == n.x)
                return y > n.y ;
            return x > n.x ;
        }	    
    };
    typedef struct Node SNode;
    
    struct NodeComparator
    {
        bool operator()(const Node* nl,const Node* nr)
        {
            if (nl->x == nr->x)
                return nl->y > nr->y;
            return nl->x > nr->x;
        }
    };
    typedef struct NodeComparator SNodeComparator;
    
    struct NodeFinder
    {
    	 int x ;
    	 int y ;
    	 NodeFinder(int a,int b):x(a),y(b){}
    	 
    	 //此处不能使用const SNode&
    	 bool operator()(const SNode* pNode) const
    	 {
    	 	    return pNode->x == x && pNode->y == y ;
    	 }
    };
    typedef struct NodeFinder SNodeFinder;
    
    int main(int argc, char* argv[])
    {
    	  deque<SNode* > q;
    	  
        q.push_back(new SNode(10,12));
        q.push_back(new SNode(15,12));
        q.push_back(new SNode(20,12));
        q.push_back(new SNode(35,12));
        q.push_back(new SNode(25,12));
        q.push_back(new SNode(10,22));
        
        NodeComparator nodeComparator;
            
        std::cout << "make heap" <<std::endl;
        make_heap(q.begin(),q.end(),nodeComparator);
        cout << "front " << q.front()->x << "  "<<q.front()->y <<endl;
            
        for (unsigned int i = 0; i< q.size(); i++)
        	 cout << q[i]->x <<"   "<< q[i]->y << endl;    		
        		
        std::cout << "pop heap" <<std::endl;
        pop_heap(q.begin(),q.end(),nodeComparator);
        q.pop_front();
        for(unsigned int i = 0; i< q.size(); i++)
        	 std::cout << q[i]->x <<"   "<< q[i]->y << std::endl;
        
        deque<SNode* >::iterator it = find_if(q.begin(),q.end(),SNodeFinder(20,12));
        if(it != q.end())
        {
        	  q.erase(it);
        	  std::cout << "removed the found node."<<std::endl;
        }
        
        make_heap(q.begin(),q.end(),nodeComparator);
        sort_heap(q.begin(),q.end(),nodeComparator);
        for (unsigned int i = 0; i< q.size(); i++)
        	std::cout << q[i]->x <<"   "<< q[i]->y << std::endl;
        
        std::cout << '\n';
        return 0;
    }
    

      

  • 相关阅读:
    CF850A Five Dimensional Points 题解
    AT3963 [AGC024F] Simple Subsequence Problem 题解
    jquery事件绑定机制
    前端初级词汇
    一个checkbox细节处理方式(checkbox与后面的文字对其)
    转 CSS hack:针对IE6,IE7,firefox显示不同效果
    想到的几点页面规范
    jQuery UI Dialog:Demo2:实用技巧
    zindex
    递归示例
  • 原文地址:https://www.cnblogs.com/syru/p/3017122.html
Copyright © 2011-2022 走看看