c++11 标准模板(STL)(std::forward_list)(十一)

定义于头文件 <forward_list>

template<

    class T,
    class Allocator = std::allocator<T>

> class forward_list;
(1)(C++11 起)
namespace pmr {

    template <class T>
    using forward_list = std::forward_list<T, std::pmr::polymorphic_allocator<T>>;

}
(2)(C++17 起)

std::forward_list 是支持从容器中的任何位置快速插入和移除元素的容器。不支持快速随机访问。它实现为单链表,且实质上与其在 C 中实现相比无任何开销。与 std::list 相比,此容器提在不需要双向迭代时提供更有效地利用空间的存储。

在链表内或跨数个链表添加、移除和移动元素,不会非法化当前指代链表中其他元素的迭代器。然而,在从链表移除元素(通过 erase_after )时,指代对应元素的迭代器或引用会被非法化。

std::forward_list 满足容器 (Container) (除了 operator== 的复杂度始终为线性和 size 函数)、具分配器容器 (AllocatorAwareContainer) 和序列容器 (SequenceContainer) 的要求。
 

操作

合并二个已排序列表

std::forward_list<T,Allocator>::merge

void merge( forward_list& other );

(1)(C++11 起)

void merge( forward_list&& other );

(1)(C++11 起)

template <class Compare>
void merge( forward_list& other, Compare comp );

(2)(C++11 起)

template <class Compare>
void merge( forward_list&& other, Compare comp );

(2)(C++11 起)

 

归并二个已排序链表为一个。链表应以升序排序。

不复制元素。操作后容器 other 变为空。若 other 与 *this 指代同一对象则函数不做任何事。若 get_allocator() != other.get_allocator() ,则行为未定义。没有引用和迭代器变得非法,除了被移动元素的迭代器现在指代到 *this 中,而非到 other 中,第一版本用 operator< 比较元素,第二版本用给定的比较函数 comp

此操作是稳定的:对于二个链表中的等价元素,来自 *this 的元素始终前驱来自 other 的元素,而且 *thisother 的等价元素顺序不更改。

参数

other-要交换的另一容器
comp-比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即序于)第二参数则返回 ​true 。

比较函数的签名应等价于如下:

 bool cmp(const Type1 &a, const Type2 &b);

虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 Type1Type2 的值,无关乎值类别(从而不允许 Type1 & ,亦不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起))。
类型 Type1 与 Type2 必须使得 forward_list<T,Allocator>::const_iterator 类型的对象能在解引用后隐式转换到这两个类型。 ​

返回值

(无)

异常

若抛出异常,则此函数无效果(强异常保证),除非异常来自比较函数。

复杂度

至多 std::distance(begin(), end()) + std::distance(other.begin(), other.end()) - 1 次比较。

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <time.h>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }

    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));;

    auto generate = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };

    std::forward_list<Cell> forward_list1(3);
    std::generate(forward_list1.begin(), forward_list1.end(), generate);
    forward_list1.sort();
    std::cout << "forward_list1:    ";
    std::copy(forward_list1.begin(), forward_list1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::forward_list<Cell> forward_list2(3);
    std::generate(forward_list2.begin(), forward_list2.end(), generate);
    forward_list2.sort();
    std::cout << "forward_list2:    ";
    std::copy(forward_list2.begin(), forward_list2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //归并二个已排序链表为一个。链表应以升序排序
    forward_list1.merge(forward_list2);
    std::cout << "merge:            ";
    std::copy(forward_list1.begin(), forward_list1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::forward_list<Cell> forward_list3(3);
    std::generate(forward_list3.begin(), forward_list3.end(), generate);
    forward_list3.sort();
    std::cout << "forward_list3:    ";
    std::copy(forward_list3.begin(), forward_list3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::forward_list<Cell> forward_list4(3);
    std::generate(forward_list4.begin(), forward_list4.end(), generate);
    forward_list4.sort();
    std::cout << "forward_list4:    ";
    std::copy(forward_list4.begin(), forward_list4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //归并二个已排序链表为一个。链表应以升序排序;移动语义
    forward_list3.merge(std::move(forward_list4));
    std::cout << "merge:            ";
    std::copy(forward_list3.begin(), forward_list3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;



    auto func_sort = [](const Cell & a, const Cell & b)
    {
        if (a.x == b.x)
        {
            return a.y > b.y;
        }
        return a.x > b.x;
    };

    std::forward_list<Cell> forward_list5(3);
    std::generate(forward_list5.begin(), forward_list5.end(), generate);
    forward_list5.sort(func_sort);
    std::cout << "forward_list5:    ";
    std::copy(forward_list5.begin(), forward_list5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::forward_list<Cell> forward_list6(3);
    std::generate(forward_list6.begin(), forward_list6.end(), generate);
    forward_list6.sort(func_sort);
    std::cout << "forward_list6:    ";
    std::copy(forward_list6.begin(), forward_list6.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //归并二个已排序链表为一个。链表应以降序排序
    forward_list5.merge(forward_list6, func_sort);
    std::cout << "merge:            ";
    std::copy(forward_list5.begin(), forward_list5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::forward_list<Cell> forward_list7(3);
    std::generate(forward_list7.begin(), forward_list7.end(), generate);
    forward_list7.sort(func_sort);
    std::cout << "forward_list7:    ";
    std::copy(forward_list7.begin(), forward_list7.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::forward_list<Cell> forward_list8(3);
    std::generate(forward_list8.begin(), forward_list8.end(), generate);
    forward_list8.sort(func_sort);
    std::cout << "forward_list8:    ";
    std::copy(forward_list8.begin(), forward_list8.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //归并二个已排序链表为一个。链表应以降序排序,移动语义
    forward_list7.merge(std::move(forward_list8), func_sort);
    std::cout << "merge:            ";
    std::copy(forward_list7.begin(), forward_list7.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}

输出

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值