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) 的要求。
 

成员函数

构造 forward_list

forward_list();

(1)

explicit forward_list( const Allocator& alloc );

(2)
forward_list( size_type count,

              const T& value,

              const Allocator& alloc = Allocator());
(3)(C++11 起)

explicit forward_list( size_type count );

(4)(C++11 起)
(C++14 前)

explicit forward_list( size_type count, const Allocator& alloc = Allocator() );

(C++14 起)
template< class InputIt >

forward_list( InputIt first, InputIt last,

              const Allocator& alloc = Allocator() );
(5)(C++11 起)

forward_list( const forward_list& other );

(6)(C++11 起)

forward_list( const forward_list& other, const Allocator& alloc );

(6)(C++11 起)

forward_list( forward_list&& other );

(7)(C++11 起)

forward_list( forward_list&& other, const Allocator& alloc );

(8)(C++11 起)

forward_list( std::initializer_list<T> init,
              const Allocator& alloc = Allocator() );

(9)(C++11 起)

从各种数据源构造新容器,可选地使用用户提供的分配器 alloc

1) 默认构造函数。构造拥有默认构造的分配器的空容器。

2) 构造拥有给定分配器 alloc 的空容器。

3) 构造拥有 count 个有值 value 的元素的容器。

4) 构造拥有个 count 默认插入的 T 实例的容器。不进行复制。

5) 构造拥有范围 [first, last) 内容的容器。

InputIt 是整数类型,则此构造函数拥有的效果同 forward_list(static_cast<size_type>(first), static_cast<value_type>(last), a) 。

(C++11 前)

此重载仅若InputIt 满足遗留输入迭代器 (LegacyInputIterator) 才参与重载决议,以避免和重载 (2) 的歧义。

(C++11 起)

6) 复制构造函数。构造拥有 other 内容的容器。若不提供 alloc ,则如同通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。

7) 移动构造函数。用移动语义构造拥有 other 内容的容器。分配器通过属于 other 的分配器移动构造获得。

8) 有分配器扩展的移动构造函数。以 alloc 为新容器的分配器,从 other 移动内容;若 alloc != other.get_allocator() ,则它导致逐元素移动。

9) 构造拥有 initializer_list init 内容的容器。

参数

alloc-用于此容器所有内存分配的分配器
count-容器的大小
value-以之初始化容器元素的值
first, last-复制元素的来源范围
other-用作初始化容器元素来源的另一容器
init-用作初始化元素来源的 initializer_list

复杂度

1-2) 常数

3-4) 与 count 成线性

5) 与 firstlast 的距离成线性

6) 与 other 的大小成线性

7) 常数。

8) 若 alloc != other.get_allocator() 则为线性,否则为常数。

9) 与 init 的大小成线性。

异常

Allocator::allocate 的调用可能抛出。

注意

在容器移动构造(重载 (7) )后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 2321 正在考虑更严格的保证。 

析构 forward_list

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

~forward_list();

(C++11 起)

销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器大小成线性。

 

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>

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;

    //1) 默认构造函数。构造拥有默认构造的分配器的空容器。
    std::forward_list<Cell> forward_list1;
    std::cout << "forward_list1 empty: " << forward_list1.empty() << std::endl;
    std::cout << std::endl;

    //3) 构造拥有 count 个有值 value 的元素的容器。
    std::forward_list<Cell> forward_list2(5, Cell{101, 101});
    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;

    //4) 构造拥有个 count 默认插入的 T 实例的容器。不进行复制。
    std::forward_list<Cell> forward_list3(5);
    std::cout << "forward_list3:    ";
    std::copy(forward_list3.begin(), forward_list3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //5) 构造拥有范围 [first, last) 内容的容器。
    std::forward_list<Cell> forward_list4(forward_list2.begin(), forward_list2.end());
    std::cout << "forward_list4:    ";
    std::copy(forward_list4.begin(), forward_list4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //6) 复制构造函数。构造拥有 other 内容的容器。
    std::forward_list<Cell> forward_list5(forward_list2);
    std::cout << "forward_list5:    ";
    std::copy(forward_list5.begin(), forward_list5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //7) 移动构造函数。用移动语义构造拥有 other 内容的容器。
    std::forward_list<Cell> forward_list6(std::move(forward_list2));
    std::cout << "forward_list2:    ";
    std::copy(forward_list2.begin(), forward_list2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "forward_list6:    ";
    std::copy(forward_list6.begin(), forward_list6.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //9) 构造拥有 initializer_list init 内容的容器。
    std::forward_list<Cell> forward_list7({Cell{101, 101}, Cell{102, 102},
                                           Cell{103, 103}, Cell{104, 104}, Cell{105, 105}});
    std::cout << "forward_list7:    ";
    std::copy(forward_list7.begin(), forward_list7.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、付费专栏及课程。

余额充值