Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 642
cppb Avatar asked Nov 22 '25 04:11

cppb


1 Answers

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;
like image 63
kennytm Avatar answered Nov 23 '25 20:11

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!