#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0

    Pointer to a vector


    I have a class that has a member that is a pointer to a two dimensional vector.

    If I have a pointer to that class: how can I access one of the elements of the vector.

    For example:

    Code:
    class myClass
    {
    public:
      vector < vector <int > > *vect;
      [...]
    
    }
    
    int funct(myClass *obj, int i, int j)
    {
      żżż??? // Here for example I want to return element (i,j) of myClass' vector
    }

    Thanks in advance
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    hi you actually dont need to pass in the myClass *obj pointer. each object of a class contains a pointer to itself call the 'this' pointer. now assuming everything has been intiailized correctly, returning the element could be donelike so:
    Code:
     return this->v_ptr->(get element method);
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0
    The problem is that funct is not a member of myClass so it can't access the *this pointer.

    Also, what do you mean by (get elementmethod)?
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243

    Does this help?


    Code:
    #include <iostream>
    #include <vector>
    
    class myClass{
    public:
        std::vector < std::vector < int > > myVect;
    };
    
    int main(){
        int i, j;
        myClass cls;
    
        //load up a bunch of data...
        for (i=0; i<5; i++){
            std::vector < int > intVect;
    
            for (j=0; j<5; j++){
                intVect.push_back((i*5)+j);
            }
            cls.myVect.push_back(intVect);
        }
    
        //read it back out using an iterator...
        std::vector < std::vector < int > >::iterator iter = cls.myVect.begin();
        std::cout << "Read data out using iterators...\n";
        while (iter != cls.myVect.end()){
            std::vector < int >::iterator iterInner = iter->begin();
            while (iterInner != iter->end()){
                std::cout << (*iterInner) << "\t";
                iterInner++;
            }
            std::cout << std::endl;
            iter++;
        }
    
        std::cout << "\nRead data out using array dereferencing...\n";
        for (i=0; i<cls.myVect.size(); i++){
            for (j=0; j<cls.myVect[i].size(); j++){
                std::cout << cls.myVect[i][j] << "\t";
            }
            std::cout << std::endl;
        }
    }
    Output:

    Code:
    Read data out using iterators...
    0       1       2       3       4
    5       6       7       8       9
    10      11      12      13      14
    15      16      17      18      19
    20      21      22      23      24
    
    Read data out using array dereferencing...
    0       1       2       3       4
    5       6       7       8       9
    10      11      12      13      14
    15      16      17      18      19
    20      21      22      23      24

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0
    That's ok but does not resolve the problem I got.

    How could that be if I change this?

    Code:
    class myClass{
    public:
        std::vector < std::vector < int > > *myVect;
    };
    Can I use array dereferencing from main function?
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    That won't work because the vector is only a pointer and doesn't point to anything. Is this what you want?

    Code:
    int myFunct(myClass *cls, int i, int j){
        return cls->myVect[i][j];
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0
    Some sort of that.

    I forgot to initialize:
    Code:
    class myClass{
    public:
        std::vector < std::vector < int > > *myVect = new std::vector < std::vector < int > >(100);
    };
    If i try to do

    Code:
    int myFunct(myClass *cls, int i, int j){
        return cls->myVect[i][j];
    }
    I'll get an error because myVect is not a vector it's a pointer to a vector so I can't use operator []
  14. #8
  15. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Why are you initializing it with a new? You will have to explicitly delete it or get a memory leak. Just let it grow by itself. I compiled and ran the code I gave you and it worked fine (on Visual C++, but I don't think the compiler matters).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0
    I'm trying to share the same vector among various threads so each must have a copy of the vector. That's why I use pointers.

    The mutual exclusion is already managed so that's not the point.
  18. #10
  19. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    i re-read your first statement and i now i think i understand what the problem is. the member in your class is a pointer to a pointer to a pointer, yes? so you would have to declare it as that. what about:
    Code:
    vector <int> ***v_ptr;
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    54
    Rep Power
    11
    Just dereference the pointer before you use operator[].

    Code:
    int myFunct(myClass *cls, int i, int j){
        return (*cls->myVect)[i][j];
    }

IMN logo majestic logo threadwatch logo seochat tools logo