heap的一点疑惑
  • 板块学术版
  • 楼主astrading
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/7/16 17:04
  • 上次更新2023/10/27 20:01:06
查看原帖
heap的一点疑惑
588042
astrading楼主2022/7/16 17:04

这是代码

#include <iostream>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
const int N = 1e6 + 10;

template<typename T>
void print(T a)
{
    for(auto i : a)
    {
        cout << i << " ";
    }
    cout << endl;
}

int main()
{
    int a[] = {10, 50, 40, 20, 30};
    vector<int> ve(a, a+5);
    print(ve);

    make_heap(ve.begin(), ve.end(), greater<int>());
    print(ve);

    pop_heap(ve.begin(), ve.end(), greater<int>());
    //ve.pop_back();
    print(ve);

    ve.push_back(100);
    push_heap(ve.begin(), ve.end(), greater<int>());
    print(ve);

    sort_heap(ve.begin(), ve.end());
    print(ve);

    return 0;
}

输出

10 50 40 20 30 
10 20 40 50 30 
20 30 40 50 10 
20 30 40 50 10 100 
10 30 50 40 100 20 

请问我在sort_heap的时候排序的是ve.begin()与ve.end(),结果为什么是 10 30 50 40 100 20?

如果取消对ve.pop_back()注释时候 结果为什么是30 40 50 100 20这里把10pop掉后20为啥会到最后面?

2022/7/16 17:04
加载中...