LearnFrenzy
Sign in Take a free test

STL Containers · Find the Error · Question 8

Error: calling front() on a priority_queue Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <queue>

int main() {
    std::priority_queue<int> pq;
    pq.push(4);
    pq.push(9);
    std::cout << pq.front() << '\n';
    return 0;
}
  • A push needs two arguments
  • B priority_queue has top(), not front(); front()/back() belong to std::queue Correct
  • C priority_queue must be a min-heap
  • D int is not comparable
Correct Answer: Option B

pq.top() returns 9, the largest element.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links