Note that the Compare parameter is defined such that it returns true if its first argument comes before its second argument in a weak ordering. But because the priority queue outputs largest elements first, the elements that "come before" are actually output last. That is, the front of the queue contains the "last" element according to the weak ordering imposed by Compare.
디폴트가 맥스힙이고 비교함수를 greater 쓰느냐 less 쓰냐
우선순위가 가장 높은걸 뽑는다 - dc App
Note that the Compare parameter is defined such that it returns true if its first argument comes before its second argument in a weak ordering. But because the priority queue outputs largest elements first, the elements that "come before" are actually output last. That is, the front of the queue contains the "last" element according to the weak ordering imposed by Compare.
https://en.cppreference.com/w/cpp/container/priority_queue
greater를 쓰면 큰 게 앞에 옴. 즉, 작은 게 뒤에 옴. 우선순위큐 입장에서만 보면 먼저 꺼낼 큰 게 뒤쪽에 있다고 생각할거임. 그래서 greater를 쓰면 작은 게 먼저 튀어나오는 최소힙이 됨. less도 마찬가지..
아하 좀 이해가 되네요 weak ordering 으로 찾아보니까 이런 내용에 대한 글이 있네요
https://stackoverflow.com/questions/32748069/the-reason-of-using-stdgreater-for-creating-min-heap-via-priority-queue
std::priority_queue
is a container adaptor; basic memory considerations make the back the preferred place for modifications (with pop_back() and push_back()) for sequence containers such as std::vector.
답변 감사합니다
테크니컬한 답변이네. 난 위에 벌레캠프 말처럼 우선순위큐는 우선순위가 높은걸 끄내니까 뒤에 있는걸 먼저꺼낸다고 생각했는데 벡터 조작같은 그런 기술적인 이유도 있군 ㄱㅅㄱㅅ
먼가 논리적으로도 말이 되고 기술적으로도 말이 되니까 설계가 잘된거 같음
내가 아는 빌트인 힙은 다 최소힙인데 왜 C++만 최대힙인지 궁금하긴함 - dc App