Search code examples
c++linked-list

linked-list in C++ how to go to "next element" using STL list


I have a very basic question. I want to use STL's list instead of creating my own linked-list ( my code is shown below)

struct myList
{

    myList *next;
    myList *previous;
};

myList->next = NULL;

Using STL list:

#include <list>

std::list<int> L;
L.push_back(1);

My question is, how to access the "next" element in STL's list?


Solution

  • std::list is a container. To access individual nodes, you need to use an iterator.

    For example, to get the head node, you use

    std::list<int>::const_iterator cit = L.begin();
    

    To move to the next node, you use

    ++ cit;