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

void push_front( const T& value );

(C++11 起)

void push_front( T&& value );

(C++11 起)

前附给定元素 value 到容器起始。

没有引用和迭代器被非法化。

参数

value-要前附的元素值

返回值

(无)

复杂度

常数。

异常

若抛出异常,则此函数无效果(强异常保证)。

 

在容器头部就地构造元素

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

template< class... Args >
void emplace_front( Args&&... args );

(C++11 起)
(C++17 前)

template< class... Args >
reference emplace_front( Args&&... args );

(C++17 起)

插入新元素到容器起始。通过 std::allocator_traits::construct 构造元素,它典型地用布置 new 在容器所提供的位置原位构造元素。将参数 args... 作为 std::forward<Args>(args)... 转发给构造函数。

没有引用和迭代器被非法化。

参数

args-转发给元素构造函数的参数
类型要求
- T (容器元素类型) 必须满足可就位构造 (EmplaceConstructible) 的要求。

返回值

(无)(C++17 前)
到被插入元素的引用。(C++17 起)

复杂度

常数。

异常

若抛异常,则此函数无效果(强异常保证)。

 

操作


将该链表的所有元素的顺序反转

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

void reverse() noexcept;

(C++11 起)

 逆转容器中的元素顺序。不非法化任何引用或迭代器。

参数

(无)

返回值

(无)

复杂度

与容器大小成线性

调用示例

#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;
    for (size_t index = 0; index < 6; index ++)
    {
        //前附给定元素 value 到容器起始。
        forward_list1.push_front(generate());
        std::cout << "forward_list1:    ";
        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_list2;
    for (size_t index = 0; index < 6; index ++)
    {
        //插入新元素到容器起始。
        forward_list2.emplace_front(std::rand() % 10 + 100, std::rand() % 10 + 100);
        std::cout << "forward_list2:    ";
        std::copy(forward_list2.begin(), forward_list2.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
    }

    std::cout << std::endl;
    std::cout << std::endl;

    std::forward_list<Cell> forward_list3(6);
    std::generate(forward_list3.begin(), forward_list3.end(), generate);
    std::cout << "reverse before :  " << std::endl;
    std::cout << "forward_list3:    ";
    std::copy(forward_list3.begin(), forward_list3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    //逆转容器中的元素顺序。不非法化任何引用或迭代器。
    forward_list3.reverse();
    std::cout << "reverse after :   " << std::endl;
    std::cout << "forward_list3:    ";
    std::copy(forward_list3.begin(), forward_list3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

输出

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值