STL - 容器 - Forward List

forward list是一个行为受限的list, 不能走回头路。

它只提供前向迭代器, 而不提供双向迭代器。

eg:

rbegin(), rend(), crbegin(), crend()这些都不提供。

它不提供size()成员函数。

没有指向最末元素的anchor, 因此不提供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()");
}

运行结果:

---------------- findDemo(): Run Start ----------------
1 42 2 3 4 5 97 98 99
---------------- findDemo(): Run End ----------------

自定义find_before_if操作:

#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()");
}

运行结果:

---------------- findBeforeDemo(): Run Start ----------------
1 42 2 3 4 5 97 98 99
---------------- findBeforeDemo(): Run End ----------------

完整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);
}

运行结果:

---------------- 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 ----------------

转载于:https://www.cnblogs.com/davidgu/p/4891990.html

猜你喜欢

转载自blog.csdn.net/weixin_34167043/article/details/93803026