#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace
std;class Cmp { public: bool operator()(string a, string b){ return a.length()>=b.length(); } }; int main(int argc, char const *argv[]) { std::vector<int> arr={1,6,5,4,2,3}; //构造最大堆 make_heap(arr.begin(), arr.end(), less<int>()); for(int num:arr)cout<<num<<" "; cout<<endl; arr.push_back(0); push_heap(arr.begin(),arr.end(), less<int>());//vector push之后需要调整堆 for(int num:arr)cout<<num<<" "; cout<<endl; //堆pip_back()操作顺序 pop_heap(arr.begin(), arr.end(), less<int>()); cout<<arr.back()<<endl; arr.pop_back(); for(int num:arr)cout<<num<<" "; cout<<endl; //构造最小堆 std::vector<int> arr2={1,6,5,4,2,3}; make_heap(arr2.begin(), arr2.end(), greater<int>()); for(int num:arr2)cout<<num<<" "; cout<<endl; std::vector<string> arr3={"aa","bbb","cdcc","cedef"}; make_heap(arr3.begin(), arr3.end(), Cmp());//自定义排序 for(string num:arr3)cout<<num<<" "; cout<<endl; return 0; }