creating a vector with references to some of the elements of another vector

Posted by memC on Stack Overflow See other posts from Stack Overflow or by memC
Published on 2010-05-15T06:22:52Z Indexed on 2010/05/15 6:34 UTC
Read the original article Hit count: 165

Filed under:
|
|

hi,

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 put a reference 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;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl