定义于头文件 <forward_list>
| template<     class T, | (1) | (C++11 起) | 
| namespace pmr {     template <class 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>::begin,
std::forward_list<T,Allocator>::cbegin| iterator begin() noexcept; | (C++11 起) | |
| const_iterator begin() const noexcept; | (C++11 起) | |
| const_iterator cbegin() const noexcept; | (C++11 起) | 
返回指向容器首元素的迭代器。
若容器为空,则返回的迭代器将等于 end() 。

参数
(无)
返回值
指向首元素的迭代器。
复杂度
常数。
返回指向容器尾端的迭代器
std::forward_list<T,Allocator>::end,
std::forward_list<T,Allocator>::cend| iterator end() noexcept; | (C++11 起) | |
| const_iterator end() const noexcept; | (C++11 起) | |
| const_iterator cend() const 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(6);
    for (std::forward_list<Cell>::iterator it = forward_list1.begin(); it != forward_list1.end(); it++)
    {
        *it = generate();
    }
    std::cout << "forward_list1:    ";
    for (std::forward_list<Cell>::const_iterator it = forward_list1.cbegin(); it != forward_list1.cend(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    return 0;
}输出

返回指向第一个元素之前迭代器
std::forward_list<T,Allocator>::before_begin, cbefore_begin| iterator before_begin() noexcept; | (C++11 起) | |
| const_iterator before_begin() const noexcept; | (C++11 起) | |
| const_iterator cbefore_begin() const noexcept; | (C++11 起) | 
返回指向首元素前一元素的迭代器。此元素表现为占位符,试图访问它会导致未定义行为。仅有的使用情况是在函数 insert_after() 、 emplace_after() 、 erase_after() 、 splice_after() 和迭代器自增中:自增始前迭代器准确地给出与从 begin()/cbegin() 获得者相同的迭代器。
参数
(无)
返回值
指向首元素前一元素的迭代器。
复杂度
常数。
调用示例
#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.insert_after(forward_list1.before_begin(), generate());
        std::cout << "forward_list1:    ";
        std::copy(forward_list1.begin(), forward_list1.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
    }
    forward_list1.emplace_after(forward_list1.before_begin(), generate());
    std::cout << "forward_list1:    ";
    std::copy(forward_list1.begin(), forward_list1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    forward_list1.erase_after(forward_list1.before_begin());
    std::cout << "forward_list1:    ";
    std::copy(forward_list1.begin(), forward_list1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}
输出




















