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

  • Home
  • SEARCH
  • 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 8931507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:07:43+00:00 2026-06-15T09:07:43+00:00

For certain objects (elements) in the vector aArray , I want to create a

  • 0

For certain objects (elements) in the vector aArray, I want to create a mininum sorted heap.

  1. I want to modify the members of aArray in other functions and call Heap member functions to update the min heap. This means that I should pass in a constant vector to the Heap structure, but I am having a lot of trouble with this am being swamped with errors. What is the proper way to pass constant objects to Heap constructor?

  2. Also, when I call the function popHeap, I want to delete only the pointer to the root in the heap, but it deletes the object in vector aArray also. How can I fix this?

    #include <vector>
    #include <iostream>
    
    struct A
    {
        A(int av, int bv):a(av),b(bv){}
        int a, b;
    };
    
    struct Heap
    {
       Heap() : ptr(new std::vector<A>()) {}
    
       Heap(std::vector<A> *p) : ptr(p) {}
    
       void makeHeap()
       { // some code here }
    
       void popHeap()
       {
           ptr->erase(ptr.begin()+heapLoc[0]);  //DELETES aArray member! I only want to delete the pointer, not the object. 
           //some code here   
       }
    
       std::vector<A> *ptr;
       std::vector<int> heapLoc;
    };
    
    
    int main()
    {
         A a0(2,5), a1(4,2), a2(8,4), a3(0,3);  
         std::vector<A> aArray;  
         aArray.push_back(a0);  aArray.push_back(a1);  aArray.push_back(a2);   aArray.push_back(a3);
    
         for(int i=0; i<aArray.size(); ++i)
         {
            std::cout << "aArray[i].a = " << aArray[i].a << " "
                      << "aArray[i].b = " << aArray[i].b <<  "\n";
         } //All 4 objects outputted
    
         Heap h(&aArray);
         h.makeHeap();
    
         h.popHeap();
    
         for(int i=0; i<aArray.size(); ++i)
         {
            std::cout << "aArray[i].a = " << aArray[i].a << " "
                      << "aArray[i].b = " << aArray[i].b <<  "\n";
         } //Only 3 objects outputted. One gets deletated. 
    }
    
  • 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-15T09:07:43+00:00Added an answer on June 15, 2026 at 9:07 am

    Copy the entire passed vector in the constructor (don’t just have a pointer to it). Allowing anything to modify the internal structure of something that needs to have a specific structure (as in sorted elements) to work is a bad idea. You can do this simply as follows:

    class Heap
    {
       ...
       Heap(std::vector<A> &input): vect(input) { };
       std::vector<A> vect;
    }
    

    You will need to either have a copy constructor or override the = operator for A for the above to work, but doing both doesn’t hurt. As in:

    class A
    {
       ...
       A(const A &o): a(o.a), b(o.b) {}; // Copy constructor
       void operator=(const A &o): a(o.a), b(o.b) {}; // = operator
    }
    

    Adding both pieces of code will copy all the elements in the vector in the heap’s constructor. Pointers won’t work for this since are always be shallow-copied, unless you explicitly deep-copy them, which is a bit more effort.

    An alternative to this is always starting with an empty heap and having an add method (still not using pointers).

    If you don’t want to copy elements, you can use the const keyword. As in use const A everywhere in your code where you used A.

    Note 1 – I’d suggest getting rid of the makeHeap method and just doing that work in the constructor.

    Note 2 – I used class above, and not struct. Don’t use struct in C++, use class instead. struct is more C.

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

Sidebar

Related Questions

I have a PHP stuff that uses call_user_func to create element/objects to a certain
I want get all of the Geom objects that are related to a certain
I'm looking to create an array or list with elements of a certain type
When I write tests for certain types of objects, such as UI elements like
I'm trying to understand when I can put certain objects into a users session
What is the best way to store settings for certain objects in my database?
So I was pondering about certain problem involving serialization of objects to relational databases.
I need a collection of objects which can be looked up by a certain
I have an application which creates objects of a certain kind (let's say, of
Is there any way that we can mock certain methods using existing objects ?

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.