forward_list
Singly Linked List
No direct access to the elements. To access n-th element, iterate through n-1 elements.
By design, it is as efficient as a simple handwritten C-style singly-linked list, and in fact is the only standard container to deliberately lack a size member function for efficiency considerations: due to its nature as a linked list, having a size member that takes constant time would require it to keep an internal counter for its size (as list does)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include <forward_list> forward_list<int > fl; forward_list<int > init_fl = {2 , 3 , 4 , 5 }; fl.push_front(1 ); fl.push_front(2 ); fl.push_front(3 ); fl.push_front(4 ); fl.pop_front(); cout << "front = " << fl.front() << "\n" ;fl.insert_after(fl.begin(), 4 ); fl.insert_after(fl.before_begin(), 4 ); fl.erase_after(fl.begin()); fl.remove(1 ); fl.sort(); fl.unique(); init_fl.reverse(); if (fl.empty()) cout << "forward list is empty" << "\n" ; else cout << "forward list is not empty" << "\n" ; for (int & ele: fl) { cout << ele << "\n" ; } for (auto it = init_fl.begin(); it != init_fl.end(); ++it) { cout << *it << "\n" ; } fl.merge(init_fl);
list
Doubly Linked List
No direct access to the elements. To access n-th element, iterate through n-1 elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include <list> list <int > l;list <int > init_list = {1 , 3 , 2 , 4 };l.push_front(1 ); l.push_front(2 ); l.push_front(3 ); l.push_back(4 ); l.push_back(5 ); l.push_back(6 ); l.pop_front(); l.pop_back(); cout << "front = " << l.front() << "\n" ;cout << "back = " << l.back() << "\n" ;cout << "size = " << l.size() << "\n" ;init_list.sort(); init_list.sort(greater<int >()); init_list.reverse(); auto it = init_list.erase(init_list.begin());init_list.insert(it, 1 ); for (const int & ele: init_list) cout << ele << "\n" ; for (auto it = l.begin(); it != l.end(); ++it) cout << *it << "\n" ; for (auto it = l.rbegin(); it != l.rend(); ++it) cout << *it << "\n" ;
References