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 8902683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:40:31+00:00 2026-06-15T01:40:31+00:00

I have a Nx by Ny vector of objects A . A has members

  • 0

I have a Nx by Ny vector of objects A.

  • A has members a and b.
  • If member b meets a certain criteria, it is added to a vector of pointers heapItem.
  • I then want to use the function std::make_heap to create a minimum heap.

Then, in my code, I want to change the value of A[i][j].b present in the heap and want the heap to reflect these changes.

For this, I would need to write a filterUp and filterDown routine. My problem is that I do not know the location of the A[i][j].b in the heap. Is there any way I can find out or another way to write the trickleUp & trickleDown routines? I don’t want to constantly call the make_heap function for it might be costly.

#include <vector>
#include <iostream>
#include <algorithm>
#include <cmath>

struct A
{
  A(){}

  A(int av, int bv):a(av),b(bv){}

  int a, b;
};

struct MinHeap
{
    bool operator()(const A* lhs, const A* rhs) const
    {
        return lhs->b > rhs->b;
    }
};

int main()
{
  int Nx=4;
  int Ny=3;
  int cnt=0;
  std::vector<A*> heapItem;

  A gridPt[Nx][Ny];  
  for(int i=0; i<Nx; ++i)  // initialize grid of A objects
  {
    for(int j=0; j<Ny; ++j)
    {
      gridPt[i][j].a=i*(-j)+2*j+i+j;
      gridPt[i][j].b=i+j*(-2)+4;

      if(gridPt[i][j].b>0)  // add selected A objects to heap
      { 
          heapItem.push_back(&gridPt[i][j]);
          cnt++;
      }
    }
  }

    std::make_heap(heapItem.begin(), heapItem.end(), MinHeap());  //make heap

    gridPt[1][2].b=3; //this object is in heap. need to call a filterUp or filterDown routine to retain min heap structure

    int count=0;
    for(int i=0; count<heapItem.size(); ++i)
    {
        for(int j=0; j<pow(i,2) && count<heapItem.size(); ++j)
        {
            std::cout << heapItem[count++]->b << "   ";
        }
        std::cout << std::endl;
    }

    //make_heap, push_heap, pop_heap maintain min heap structure

  return 0;

}
  • 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-06-15T01:40:32+00:00Added an answer on June 15, 2026 at 1:40 am

    You have a heap of A* pointers. You need the index of an A* obj in the heap in order to preserve heap structure after manipulating obj. There is no way to get it other than searching the entire heap. Here are some options:

    1. Only manipulate the top of the heap, where it is obvious how to to get the index. This is how you’re supposed to use non-intrusive heaps.
    2. Implement your own intrusive heap where struct A has an index member. You need your own heap functions which update it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of pointers to class objects. These class objects invoke new
I have a vector of pointers to objects. I'd like to remove objects from
I have a vector of pointers to Mouse objects called 'mice'. I'm passing the
I have a vector for storing the pointers of all objects (created by new
I have an Application object that has a vector of GameChar objects as one
A similar question has been asked here: Class members that are objects - Pointers
I have a class that has a vector of another class objects as a
I have a class A, which has a static vector of objects. The objects
I have a set of objects in a vector of pointers to their baseclass
I have a vector of objects (objects are term nodes that amongst other fields

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.