A good way to stay flexible is to write less code. Pragmatic Programmer
The forward_list is a container that implement on single linked list. The singly linked list can be iterated only forward.
![]() |
forward_list objects are single-linked lists. |
![]() |
It iterate the elements only forward. |
![]() |
Cannot access the items using index. |
#include "stdafx.h" #include < iostream > #include < forward_list > using namespace std; int _tmain(int argc, _TCHAR* argv[]) { forward_list < int > list1; cout << "Store items in list" << endl; for (int i = 0; i < 10; i++) list1.push_front(i); forward_list < int >::iterator iter; cout << "Display items from list : "; for (iter = list1.begin(); iter != list1.end(); ++iter) cout << *iter << " "; cout << endl; list1.reverse(); cout << "Reverse the list items and display from list : "; for (iter = list1.begin(); iter != list1.end(); ++iter) cout << *iter << " "; cout << endl; return 0; }
Store items in list Display items from list: 9 8 7 6 5 4 3 2 1 0 Reverse the list items and display from list: 0 1 2 3 4 5 6 7 8 9
When you need fast insertion and removal, you can use forward_list.
0 Comments