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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:09:41+00:00 2026-06-15T05:09:41+00:00

I am confused on how to use pointers and vector pointers to perform the

  • 0

I am confused on how to use pointers and vector pointers to perform the correct operation.

I want to pass elements of vector Vec to struct function updateHeap. updateHeap modifies the value of these members. I can’t quite achieve this result with my code below. I have commented sections in my code to explain my problem better.

#include <vector>
#include <iostream>

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

    int a, b;
    int hLoc;
};

struct Heap
{
    Heap() : heapMembers(new std::vector<A>) {}

    Heap(std::vector<A> *members) : heapMembers(members) {}

    void updateHeap(unsigned int idx)
    {
        (*heapMembers)[idx].a*=2;   
    }

    std::vector<A> *heapMembers; //ptr
};

int main()
{
    std::vector<A> aVec;  
    //I want to use updateHeap function in struct Heap to modify values of select elements of aVec


    A a0(2,5), a1(4,2), a2(8,4);  
        aVec.push_back(a0); aVec.push_back(a1); aVec.push_back(a2); 
        //Here I initialized aVec


    Heap minHeap1;
    //In minHeap1, I want to add elements one by one

    minHeap1.heapMembers->push_back(aVec[1]); 
    //I added a selected element (aVec[1]) in aVec to the heap; 
    minHeap1.updateHeap(minHeap.heapMembers->size()-1); 
    //I want to modify the value of aVec[1].a by calling updateHeap function
    std::cout << aVec[1].a << "\n";
    //Output: 4 , I want this value to be 2*4=8. 

    std::vector<A> heapVec;  
    heapVec.push_back(a1);

    Heap minHeap2(&heapVec);
    //In minHeap2, I want to give it a vector of selected elements
    minHeap2.updateHeap(minHeap2.heapMembers->size()-1);
    //I want to modify the value of aVec[1].a by calling updateHeap function
    std::cout <<    aVec[1].a << "\n";
    //Output: 4 , I want this value to be 2*4=8.
    std::cout << heapVec[0].a << "\n";
    //Output: 8 , This is the right answer, but I want aVec[1].a to take upon this value

    delete minHeap1.heapMembers;

    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-15T05:09:42+00:00Added an answer on June 15, 2026 at 5:09 am

    Your problem is that vector::push_back() copies the pushed element into the vector; it does not push a reference nor anything like that. So after:

    minHeap1.heapMembers->push_back(aVec[1]);
    

    (*minHeap1.heapMembers)[0] will be a copy of aVec[1]. Changing that (as you do in Heap::updateHeap()) changes the copy, not the original.

    If you want to be able to change aVec through minHeap1, you will need to actually use that vector inside the heap (more or less what you do with heapVec and minHeap2) or change the vector inside Heap so it does not contain values, but references or pointers. You cannot have an std::vector of references, so you will have to do with pointers:

    struct Heap
    {
        Heap() : heapMembers(new std::vector<A*>) {}
    
        Heap(std::vector<A*> *members) : heapMembers(members) {}
    
        void updateHeap(unsigned int idx)
        {
            (*heapMembers)[idx]->a*=2;   
        }
    
        std::vector<A*> *heapMembers; //ptr
    };
    

    And then, in your main() function:

    minHeap1.heapMembers->push_back(&aVec[1]); 
    

    Of course, this will create some problems with the lifetime of the elements pointed to by the pointers inside heapMembers, but your main() will work as long as you take care to make heapVec also an std::vector<A*>.

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

Sidebar

Related Questions

For pointers, I'm getting confused with declarations and function parameters on when to use
Today i was reading about pure function, got confused with its use: A function
I am confused why codeigniter wont let me use this: $(function() { var csrf
I'm learning the use of boost smart pointers but I'm a bit confused about
I thought that the correct type to use to store the difference between pointers
I want to create a link list in C++.I am confused whether to use
I'm trying to use function pointers and Abstract data types in c. This is
i'm a bit confused, when we release a pointer or use nil to the
i am creating a big application (ERP system) and i am confused to use
I'm a bit confused about the use of all the IEnumerable<T> extension methods, intellisense

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.