zoukankan      html  css  js  c++  java
  • 算法库中heap应用

    STL中make_heap 的接口为:

     

    default (1)
    template <class RandomAccessIterator>
      void make_heap (RandomAccessIterator first, RandomAccessIterator last);
    
    custom (2)
    template <class RandomAccessIterator, class Compare>
      void make_heap (RandomAccessIterator first, RandomAccessIterator last,
                      Compare comp );

    默认的使用operator< 进行比较。而我们可以自定义comp进行比较,来进行建堆。

     

    其中,两个make_heap所使用的参数,[first,last)     这个区间是半开半闭的。

    当我们需要对堆进行存取操作时,我们有函数,pos_heap,push_heap

     

    default (1)
    template <class RandomAccessIterator>
      void pop_heap (RandomAccessIterator first, RandomAccessIterator last);
    
    custom (2)
    template <class RandomAccessIterator, class Compare>
      void pop_heap (RandomAccessIterator first, RandomAccessIterator last,
                     Compare comp);



    使用pop_heap 操作后, 最大值被移动到last-1的位置。[first ,last-1) 之间的元素继续保持堆的形态。

     

    我们只需取出最后一个元素即可。也就是vec.pop_back();

    default (1)
    template <class RandomAccessIterator>
      void push_heap (RandomAccessIterator first, RandomAccessIterator last);
    
    custom (2)
    template <class RandomAccessIterator, class Compare>
      void push_heap (RandomAccessIterator first, RandomAccessIterator last,
                       Compare comp);

    执行push_heap 时, [first,last-1)个元素是保持堆形态的,如果不是堆,则会报错。

     

    这个函数是相当于对堆进行调整。

    Code:

    
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    using namespace std;
    vector heap;
    int n;
    int main()
    {
    	cout<<"Insert numbers of heap:";
    	cin>>n;
    	cout<<"Insert values of each members:
    ";
    	for(int i=0;i<n;i++)
    	{
    		int read;
    		cin>>read;
    		heap.push_back(read);
    	}
    	make_heap(heap.begin(),heap.end());//大根堆
    	//make_heap(heap.begin(),heap.end(),greater());小根堆
    	cout<<"If there's something trouble,insert "help"!
    ";
    	cout<<"Heap has neen maken!
    Please insert commands:
    ";
    	while(1)
    	{
    		string s;
    		cin>>s;
    		if(s=="pop")
    		{
    			pop_heap(heap.begin(),heap.end());
    			cout<<heap[heap.size()-1];
    			heap.pop_back();
    			cout<<endl;
    		}
    		else if(s=="push")
    		{
    			int read;
    			cin>>read;
    			heap.push_back(read);
    			push_heap(heap.begin(),heap.end());
    		}
    		else if(s=="top")
    		{
    			cout<<heap.front()<<endl;
    		}
    		else if(s=="sort")//奇怪的是,这里大根堆的堆排序是由小到大的(就是把数列逆转了)
    		{
    			vector temp;
    			temp=heap;
    			sort_heap(temp.begin(),temp.end());
    			for(int i=0;i<temp.size();i++)
    			cout<<temp[i]<<' ';
    			cout<<endl;
    		}
    		else if(s=="out")
    		{
    			for(int i=0;i<heap.size();i++)
    			cout<<heap[i]<<' ';
    			cout<<endl;
    		}
     		else if(s=="end")
    		{
    			return 0;
    		}
    		else if(s=="help")
    		{
    			cout<<"1.Pop the top number of heap,please insert "pop"!
    ";
    			cout<<"2.Push a number into heap,please insert "push numbers"!
    ";
    			cout<<"3.Look the top number of heap,please insert "top"!
    ";
    			cout<<"4.Sort the heap and import result,please insert "sort"!
    ";
    			cout<<"5.Look the array of heap,please insert "out"!
    ";
    			cout<<"6.End the program,please insert "end"!
    ";
    		}
    		else
    		cout<<"Bad Commands!"<<endl;
    	}
    	return 0;
    } 
    
  • 相关阅读:
    3星|《全球电商进化史》:全球电商亲历记
    2星|陈春花《共生》:逻辑差语文差缺证据。不敢相信知名商学院教授的书居然这么差
    3星|《第五次开始》:考古学家写的人类简史与未来简史
    4星|《财经》2018年第21期:互联网处方能解决药品质量和价格问题
    2.5星|托夫勒《权力的转移》;30年旧书,现在看理论有点牵强肤浅,预测有的准有的不准
    2018左其盛好书榜(截至9月15日)
    沟通交流技巧相关的11本书点评
    没睡好觉的上级更容易辱骂下属:3.5星|《哈佛商业评论》第9期
    3星|《利润模式》:20年旧书,30种模式
    在 C# 中通过 P/Invoke 调用Win32 DLL
  • 原文地址:https://www.cnblogs.com/widerg/p/7074185.html
Copyright © 2011-2022 走看看