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

template< class... Args >
iterator emplace_after( const_iterator pos, Args&&... args );

(C++11 起)

在容器中的指定位置后插入新元素。原位构造元素,即不进行复制或移动操作。准确地以与提供给函数者相同的参数调用元素的构造函数。

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

参数

pos-新元素将构造于其后的迭代器
args-转发给元素构造函数的参数

返回值

指向新元素的迭代器。

复杂度

常数。

异常

若抛出任何异常(例如由构造函数),则容器留在未修改状态,如同从未调用过此函数(强异常保证)。

清除内容

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

void clear() noexcept;

(C++11 起)

从容器擦除所有元素。此调用后 size() 返回零。

非法化任何指代所含元素的引用、指针或迭代器。任何尾后迭代器保持合法。

参数

(无)

返回值

(无)

复杂度

与容器大小,即元素数成线性。

 

改变容器中可存储元素的个数

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

void resize( size_type count );

(1)

void resize( size_type count, const value_type& value );

(2)

 重设容器大小以容纳 count 个元素。

若当前大小大于 count ,则减小容器为其首 count 个元素。

若当前大小小于 count

1) 则后附额外的默认插入的元素

2) 则后附额外的 value 的副本

参数

count-容器的大小
value-用以初始化新元素的值
类型要求
- 为使用重载 (1) , T 必须满足可默认插入 (DefaultInsertable) 的要求。
- 为使用重载 (2) , T 必须满足可复制插入 (CopyInsertable) 的要求。

返回值

(无)

复杂度

与当前大小和 count 间的差成线性。可能有遍历链表以抵达首个要擦除元素/插入位置结尾所致的额外复杂度。

调用示例

#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 < 3; index ++)
    {
        //在容器中的指定位置后插入新元素。
        forward_list1.emplace_after(forward_list1.before_begin(), std::rand() % 10 + 100, std::rand() % 10 + 100);
        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;
    //从容器擦除所有元素。此调用后 size() 返回零。
    forward_list1.clear();
    std::cout << "forward_list1 empty : " << forward_list1.empty() << std::endl;
    std::cout << "forward_list1 size  : " << std::distance(forward_list1.begin(), forward_list1.end()) << std::endl;
    std::cout << std::endl;

    //重设容器大小以容纳 count 个元素。1) 则后附额外的默认插入的元素
    forward_list1.resize(6);
    std::cout << "forward_list1 empty : " << forward_list1.empty() << std::endl;
    std::cout << "forward_list1 size  : " << std::distance(forward_list1.begin(), forward_list1.end()) << std::endl;
    std::cout << "forward_list1:    ";
    std::copy(forward_list1.begin(), forward_list1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //重设容器大小以容纳 count 个元素。2) 则后附额外的 value 的副本
    //根据打印内容,感觉有bug
    forward_list1.resize(5, Cell{101, 101});
    std::cout << "forward_list1 empty : " << forward_list1.empty() << std::endl;
    std::cout << "forward_list1 size  : " << std::distance(forward_list1.begin(), forward_list1.end()) << std::endl;
    std::cout << "forward_list1:    ";
    std::copy(forward_list1.begin(), forward_list1.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、付费专栏及课程。

余额充值