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
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;
}
To iterate through a list we need to define iterators as follows:
#include
#include
- // std::list
using namespace std;
int main () {
list<int>
list<int>::iterator it = mylist.begin();
for(;it != mylist.end();it++){
cout << "\n" << *it;
}
return 0;
}
No comments:
Post a Comment