Thread: HELP! Accessing pointer elements of a vector1

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    4

    HELP! Accessing pointer elements of a vector!!!

    Hi!

    I’m having trouble knowing how to directly access pointer elements of a vector!

    I have declared a vector named “_stationaryVerticeList”. I have declared this vector so that each element of this vector points to a vector of floats…..
    -----------------------------------------------------------------------------------
    Code:
    typedef vector<float> VerticeList;    //A vector of Vertice Lists 
                                                               type  def
    vector<VerticeList*> _stationaryVerticeList; //A vector of Vertices 
                                                                             for this stationary   
                                                                              object
    ------------------------------------------------------------------------------------

    Now, in turn, I have made 4 float vectors (each with 3 values pushed onto them), and added/pushed them onto the _stationaryVerticeList vector……


    Code:
    VerticeList* NewVerticeList_0 = new VerticeList();
    NewVerticeList_0->push_back(45.5);
    NewVerticeList_0->push_back(33.2);
    NewVerticeList_0->push_back(77.8);
    _stationaryVerticeList.push_back(NewVerticeList_0);
    
    VerticeList* NewVerticeList_1 = new VerticeList();
    NewVerticeList_1->push_back(23.7);
    NewVerticeList_1->push_back(81.5);
    NewVerticeList_1->push_back(67.2);
    _stationaryVerticeList.push_back(NewVerticeList_1);
    
    VerticeList* NewVerticeList_2 = new VerticeList();
    NewVerticeList_2->push_back(54.2);
    NewVerticeList_2->push_back(91.2);
    NewVerticeList_2->push_back(12.9);
    _stationaryVerticeList.push_back(NewVerticeList_2);
    
    VerticeList* NewVerticeList_3 = new VerticeList();
    NewVerticeList_3->push_back(14.2);
    NewVerticeList_3->push_back(61.8);
    NewVerticeList_3->push_back(19.9);
    _stationaryVerticeList.push_back(NewVerticeList_3);
    ------------------------------------------------------------------------------

    Now, I need to know how I can directly access one of these vertice values through the _stationaryVerticeList vector! For example, let’s say that I wanted to access the second value from the third list added to the _stationaryVerticeList vector (it should equal 91.2) – how would I do this? ANY help would be GREATLY appreciated!!!!!!

    //---------Side Note! -----//
    I know the data is being read in properly, because I’ve looped though the _stationaryVerticeList vector using …

    Code:
    for(vector<VerticeList*>::iterator it1 = _stationaryVerticeList.begin(); it1 != _stationaryVerticeList.end(); it1++)
    {
      for(vector<GLfloat>::iterator it2 = (*it1)->begin(); it2 != (*it1)->end(); it2++)
      {
         cout << "*It2 is " << (*it2) << endl;
       }
    }
    …But I need to know how to directly access the values without having to go through this elaborate looping process!

    //------End of side note! -------//
    Last edited by Oz_joker; 04-23-2005 at 04:46 PM. Reason: Code tags!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I wanted to access the second value from the third list...
    One of a few ways: _stationaryVerticeList[2]->at(1)

    gg

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    “_stationaryVerticeList”. I have declared this vector so that each element of this vector points to a vector of floats…..
    That would mean:

    _stationaryVerticeList[0]
    _stationaryVerticeList[1]
    _stationaryVerticeList[2]
    _stationaryVerticeList[3]

    are all pointers to float vectors. So,

    *__stationaryVerticeList[2]

    is a float vector. And, you can access the elements of a vector using array notation:

    (*_stationaryVerticeList[2])[1]

    I don't know the precedence rules for * and [], so I added parentheses.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't forget that [] is slightly faster than at(), because it doesn't do bounds checking. [] will be undefined on an invalid index, at() will throw.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM