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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:08:29+00:00 2026-05-14T21:08:29+00:00

I have stored instances of class A in a std:vector , vec_A as vec_A.push_back(A(i))

  • 0

I have stored instances of class A in a std:vector, vec_A as vec_A.push_back(A(i)). The code is shown below.

Now, I want to store references some of the instances of class A (in vec_A) in another vector or another array. For example, if the A.getNumber() returns 4, 7, 2 , I want to store a pointer to that instance of A in another vector, say std:vector<A*> filtered_A or an array.

Can someone sow me how to do this?? Thanks!

class A {
      public:
             int getNumber();            
             A(int val);
             ~A(){};
      private:
              int num;
};

A::A(int val){
         num = val;
         };

int A::getNumber(){
    return num;
};

int main(){
    int i  =0;
    int num;    
    std::vector<A> vec_A;
    for ( i = 0; i < 10; i++){
        vec_A.push_back(A(i));
        }

    std::cout << "\nPress RETURN to continue...";
    std::cin.get();

    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-05-14T21:08:29+00:00Added an answer on May 14, 2026 at 9:08 pm

    I think the safest thing would be to have a second vector that holds indexes into the first vector:

    using std::vector;
    vector<A> main;
    vector<vector<A>::size_type> secondary;
    
    main.push_back(...);
    secondary.push_back(main.size() - 1);   // add the index of the last item
    

    Now, to look up an item you take the value in secondary and use that to index into main:

    main[secondary[...]];
    

    The reason I recommend this instead of just having secondary store a direct pointer is that every time you add to vector it may need to resize the vector which can invalidate any existing pointers:

    using std::vector;
    vector<A> vec;
    vec.push_back(A());
    
    // get a pointer to the item you just added
    A *p0 = &vec[0];
    
    // add another item
    a.push_back(A());
    
    // because a push_back() can cause the vector to resize, p0 may no
    // longer point to valid memory and cannot safely be dereferenced
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.