Blackle Mori
Blackle Mori

Reputation: 1181

Direct access to element of STL list with a pointer

Is it possible to access an element of an STL linked list directly by it's pointer? My program requires quick insertion, removal and access of elements.

Upvotes: 2

Views: 1829

Answers (2)

minus
minus

Reputation: 706

Instead of using STL linked list, you may want to define your own linked list implementation using pointers. For example:

template <class E>
struct Node {
    E data;
    Node * next;
};

So define a Node class that will be an element in the linked list. As Kerrek SB suggested, redesigning the program with iterators in mind might be quicker and better in the long term.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

STL containers use iterators instead of pointers. If you have an iterator that points to an element of your linked list, you can access element's data through it, insert at the iterator's position using the list's insert method, and delete at iterator's position using the erase method.

Upvotes: 3

Related Questions