Pages

Sunday, June 5, 2016

Read List Containers in C++

List in c++ are implemented as doubly-linked list as opposed to vectors which are implemented as dynamic arrays. As a property of doubly-linked list, list cannot be accessed with index location as we can with arrays and vectors.


To iterate through a list we need to define iterators as follows:

#include     // std::cout
#include       // std::list
using namespace std;

int main () {

list<int> mylist = {1,45,32,12,67};
list<int>::iterator it =  mylist.begin();

  for(;it != mylist.end();it++){
 cout << "\n" << *it;
  }
 return 0;
}

No comments:

Post a Comment