Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4106258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:21:11+00:00 2026-05-20T21:21:11+00:00

So, spring break begins so this is not quite important, ignore it if you’d

  • 0

So, spring break begins so this is not quite important, ignore it if you’d like.
This code is copied directly from a textbook, but there is an assertion error when it runs. And I’m wondering why?

#include <vector>
#include <cassert>
#include <iostream>
using namespace std;

template <class T> class Iterator;

template <class T> 
class Priority_queue {
public:
   Priority_queue() : c() { }
   bool empty() { return c.empty(); }
   unsigned int size() { return c.size(); }
   void push(const T & x) { c.push_back(x); }
   void pop(){ pop_heap (c.begin(), c.end()); }
   T & top() { return c.front(); }
private:
   vector<T> c;
};

template <class Iterator>
void push_heap(Iterator start, Iterator stop)
{
    unsigned int position = (stop - start) - 1;
    unsigned int parent = (position - 1)/2;

    while (position > 0 && start[position] > start[parent]){
    swap (start[position], start[parent]);
    position = parent;
    parent = (position - 1) /2;
    }
}

template <class Iterator>
void make_heap (Iterator start, Iterator stop)
{ 
    unsigned int heapsize = stop - start;
    for (int i = heapsize/2; i >= 0; i--)
    adjust_heap(start, heapsize,i);
}

template <class Iterator>
void sort_heap (Iterator start, Iterator stop)
{
    unsigned int lastPosition = stop - start - 1;
    while (lastPosition > 0) {
        swap (start[0], start [lastPosition]);
    adjust_heap(start, lastPosition, 0);
    lastPosition--;
    }
}

template <class Iterator>
void pop_heap (Iterator start, Iterator stop)
{
    unsigned int lastPosition = (stop - start) - 1;
    swap (start[0], start[lastPosition]);
    adjust_heap(start, lastPosition, 0);
}

template <class Iterator>
void adjust_heap(Iterator start, unsigned heapsize, unsigned position)
{
    while (position < heapsize) {
    unsigned int childpos = position * 2 + 1;
    if (childpos < heapsize) {
        if ((childpos + 1 < heapsize) &&
        start[childpos + 1] > start [childpos])
        childpos++;
    if (start[position] > start[childpos])
        return;
    else
        swap (start[position], start[childpos]);
    }
    position = childpos;
    }
}

template <class Iterator>
void heap_sort(Iterator start, Iterator stop)
{
    make_heap(start,stop);
    sort_heap(start,stop);
}

int main()
{
   Priority_queue<int> pq;

   assert(pq.size() == 0);
   assert(pq.empty());

   pq.push(10);
   assert(pq.top() == 10);

   pq.push(20);
   assert(pq.top() == 20);

   pq.push(30);
   assert(pq.top() == 30);

   pq.push(40);
   assert(pq.top() == 40);

   pq.push(50);
   assert(pq.top() == 50);

   pq.push(5);
   assert(pq.top() == 50);

   pq.pop();
   assert(pq.top() == 40);

   pq.pop();
   assert(pq.top() == 30);

   pq.pop();
   assert(pq.top() == 20);

   pq.pop();
   assert(pq.top() == 10);

   pq.pop();
   assert(pq.top() == 5);

   pq.pop();
   assert(pq.size() == 0);

   Priority_queue<int> pq2;
   pq2.push(30);
   pq2.push(11);
   pq2.push(7);
   pq2.pop();
   assert(pq2.top() == 11);
   pq2.pop();
   assert(pq2.top() == 7);
   pq2.pop();
   assert(pq2.empty());

   cout << "All tests passed." << endl;
}

The error is:

int main(): Assertion `pq.top() == 20′
failed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:21:12+00:00Added an answer on May 20, 2026 at 9:21 pm

    This code compiles fine for me in g++, but it’s making some fatal assumptions.

    In line 97 it does an assert(pq.top() == 20).

    The problem is pq.top() looks at the front of the vector that is the backing array of the queue.

    When you do a pq.push(x), it pushes x to the back of the queue, not the front. So if you fix the code so it pushes the value to the front, it should work fine.

    Edit 1: Did you copy and paste this code, or did you type it by hand while reading it? If you did the latter, you may have made an error. I’m looking through the code to see if I can fix it right now.

    Edit 2: Actually, did the code for the queue mean to use push_heap() instead of c.push()?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to break apart a string that always looks like this: something --
original column is like: 0.45::rafas::4.0::0.0::0.9 0.35::rasaf::4.0::110.0::1.0 and i would like to break the string
Here is a function I wrote to break a long string into lines not
I want to Remove the Line Break from the string if my string Ends
I am trying to understand this example from Thinking in Java : package c07;
Try this code I created,when I use it in Borland C++ and try the
using django 1.4, I am trying to Post and it seems like its not
I'm not sure what could be causing this. ==18270== Invalid free() / delete /
I'm struggling on this one and I'm to a point where I not making
I am calling a PL/SQL procedure like this: execute util.verify(src_schema => '&username', stab =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.