make_heap 간단 사용법

조한별·2022년 10월 13일
0
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

void Print(vector<int> vec);

int main()
{
    vector<int> vec = { 5, 3, 4 ,1, 8, 9, 10, 7, 2 };

    cout << "생성" << endl;
    make_heap(vec.begin(), vec.end(), greater<int>());

    Print(vec);

    cout << "벡터에 푸시백" << endl;
    vec.push_back(6);

    Print(vec);

    cout << "푸시 힙 후" << endl;
    push_heap(vec.begin(), vec.end(), greater<int>());

    Print(vec);

    cout << "팝 힙 후" << endl;
    pop_heap(vec.begin(), vec.end(), greater<int>());
    vec.pop_back();

    Print(vec);

    return 0;
}

void Print(vector<int> vec)
{
    for (int i : vec)
        cout << i << " ";

    cout << endl; cout << endl;
}

// i번쨰 노드의 자식 노드는 i * 2 + 1 || i * 2 + 2
//            부모 노드는 (i - 1) >> 1
// 정렬을 내림차순으로 하고 싶다면 greater대신 less를 사용하거나 비우면 된다

profile
게임프로그래머 지망생

0개의 댓글