forward list is an act restricted list, can not go back.
Before it is provided to the iterator, without providing bidirectional iterators.
eg:
rbegin (), rend (), crbegin (), crend () these are not provided.
It does not provide size () member function.
It does not point to the last element of the anchor, and therefore does not provide back (), push_back (), pop_back ().
ForwardListTest.cpp:
#include <forward_list> #include "../../Core/print.hpp" #include "ForwardListTest.h" using namespace std; void ForwardListTest::findDemo() { forward_list<int> list = { 1, 2, 3, 4, 5, 97, 98, 99 }; // find the position before the first even element auto posBefore = list.before_begin(); for (auto pos = list.begin(); pos != list.end(); ++pos, ++posBefore) { if (*pos % 2 == 0) { break; // element found } } // and insert a new element in front of the first even element list.insert_after(posBefore, 42); PRINT_ELEMENTS(list); } void ForwardListTest::run() { printStart("findDemo()"); findDemo(); printEnd("findDemo()"); }
operation result:
---------------- findDemo(): Run Start ----------------
1 42 2 3 4 5 97 98 99
---------------- findDemo(): Run End ----------------
Custom find_before_if operation:
#include "../../Core/findbefore.hpp" void ForwardListTest::findBeforeDemo() { forward_list<int> list = { 1, 2, 3, 4, 5, 97, 98, 99 }; // find the position before the first even element auto posBefore = find_before_if(list.before_begin(), list.end(), [](int i) { return i % 2 == 0; }); // and insert a new element in front of the first even element list.insert_after(posBefore, 42); PRINT_ELEMENTS(list); } void ForwardListTest::run() { printStart("findBeforeDemo()"); findBeforeDemo(); printEnd("findBeforeDemo()"); }
operation result:
---------------- findBeforeDemo(): Run Start ----------------
1 42 2 3 4 5 97 98 99
---------------- findBeforeDemo(): Run End ----------------
Complete Demo
void ForwardListTest::fullDemo() { // create two forward lists forward_list<int> list1 = { 1, 2, 3, 4 }; forward_list<int> list2 = { 77, 88, 99 }; printLists("initial:", list1, list2); // insert six new element at the beginning of list2 list2.insert_after(list2.before_begin(), 99); list2.push_front(10); list2.insert_after(list2.before_begin(), { 10, 11, 12, 13 }); printLists("6 new elems:", list1, list2); // insert all elements of list2 at the beginning of list1 list1.insert_after(list1.before_begin(), list2.begin(), list2.end()); printLists("list2 into list1:", list1, list2); // delete second element and elements after element with value 99 list2.erase_after(list2.begin()); list2.erase_after(find(list2.begin(), list2.end(), 99), list2.end()); printLists("delete 2nd and after 99:", list1, list2); // sort list1, assign it to list2, and remove duplicates list1.sort(); list2 = list1; list2.unique(); printLists("sorted and unique:", list1, list2); // merge both sorted lists into list1 list1.merge(list2); printLists("merged:", list1, list2); }
operation result:
---------------- fullDemo(): Run Start ----------------
initial:
list1: 1 2 3 4
list2: 77 88 99
6 new elems:
list1: 1 2 3 4
list2: 10 11 12 13 10 99 77 88 99
list2 into list1:
list1: 10 11 12 13 10 99 77 88 99 1 2 3 4
list2: 10 11 12 13 10 99 77 88 99
delete 2nd and after 99:
list1: 10 11 12 13 10 99 77 88 99 1 2 3 4
list2: 10 12 13 10 99
sorted and unique:
list1: 1 2 3 4 10 10 11 12 13 77 88 99 99
list2: 1 2 3 4 10 11 12 13 77 88 99
merged:
list1: 1 1 2 2 3 3 4 4 10 10 10 11 11 12 12 13 13 77 77 88 88 99 99 99
list2:
---------------- fullDemo(): Run End ----------------
Reproduced in: https: //www.cnblogs.com/davidgu/p/4891990.html