Thread: Using pointer to access list element

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    34

    Using pointer to access list element

    Hi,

    I'm new to C++, I have the following pointer declaration:

    Code:
    list<Vertex*>* v = m->get_vertices();
    now how can I access, say the first element of the vertex list.

    I appreciate any help that you guys can offer.

  2. #2
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    std::list has a begin() member function that returns an iterator that points to the first element of the container.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    34
    Thanks for the tip. But what if I wanna access other elements, e.g. 3rd element. Do I just set up a counter and make the iterator points to that position?

  4. #4
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    yes, usually by using a loop.

    if you're doing a lot of random access though, you might wanna use a vector instead.

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. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Conflicting types of typedef error
    By advocation in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2005, 06:26 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM