std::list random access iterator?

May 24, 2009 at 2:58am
is there a way to use a random access iterator with an std list?
since apparently a list won't allow access via array schematics.. i have to use iterators..
however thats completely failing cause lists also apparently don't use a random access iterator for std::list<listtype>::iterator

i did some research on iterators b4 posting..
but uhh.. everywhere i looked showed me the different catagories of iterators.. but never how to actually say which one i wanted to use.. if you even can.
May 24, 2009 at 3:33am
Lists are by definition incompatible with random access.
http://en.wikipedia.org/wiki/Linked_list

A list as a structure only knows where the first and the last nodes are, and each node knows where the next and the previous nodes are, so the only way for the structure as a whole to know where a middle node is, is by traversing them.

This is what a simplified doubly linked list looks like:
1
2
3
4
5
6
7
8
9
10
struct Node{
    T data;
    Node *previous;
    Node *next;
};

struct List{
    Node *first;
    Node *last;
};
May 24, 2009 at 4:54am
deque class then is what im gonna use.
just did some test and it seems to do everything i need.
i was using a list for its native algorithms... but now i don't even remember why... heh.
EDIT:
i just remembered why..
i need its sort function.
EDIT2:
im gonna use stls sort algorithm

problem solve.
thanks for filling me in helios
Last edited on May 24, 2009 at 5:25am
Topic archived. No new replies allowed.