c++11 标准模板(STL)(std::forward

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

定义于头文件 <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>::erase_after

iterator erase_after( const_iterator pos );

(1)(C++11 起)

iterator erase_after( const_iterator first, const_iterator last );

(2)(C++11 起)

从容器移除指定元素。

1) 移除后随 pos 的元素。

2) 移除范围 (first; last) 中的元素。

参数

pos-指向前趋要被移除元素的迭代器
first, last-要移除的元素范围

返回值

1) 指向后随被擦除元素的迭代器或若不存在这种元素则为 end() 。

2) last

复杂度

1) 常数。

2) 与 firstlast 之间的距离成线性。

 

移除首元素

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

void pop_front();

(C++11 起)

移除容器首元素。若容器中无元素则行为未定义。

指向被擦除元素的迭代器和引用被非法化。

参数

返回值

复杂度

常数。

异常

不抛出。

 

交换内容

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

void swap( forward_list& other );

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

void swap( forward_list& other ) noexcept(/* see below */);

(C++17 起)

 将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。

所有迭代器和引用保持合法。在操作后保有此容器中尾后值的迭代器指代此容器或另一容器是未指定的

若 std::allocator_traits<allocator_type>::propagate_on_container_swap::value 为 true 则用非成员 swap 的非限定调用交换分配器。否则不交换它们且若 get_allocator() != other.get_allocator() 则行为未定义。

(C++11 起)

参数

other-要与之交换内容的容器

返回值

异常

(C++17 前)
noexcept 规定  

noexcept(std::allocator_traits<Allocator>::is_always_equal::value)

(C++17 起)

复杂度

常数。

调用示例

#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);
    std::generate(forward_list1.begin(), forward_list1.end(), generate);
    std::cout << "forward_list1:    ";
    std::copy(forward_list1.begin(), forward_list1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //从容器移除指定元素。1) 移除后随 pos 的元素。
    forward_list1.erase_after(forward_list1.begin());
    std::cout << "forward_list1:    ";
    std::copy(forward_list1.begin(), forward_list1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //从容器移除指定元素。2) 移除范围 (first; last) 中的元素。
    forward_list1.erase_after(forward_list1.begin(), forward_list1.end());
    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(6);
    std::generate(forward_list2.begin(), forward_list2.end(), generate);
    for (size_t index = 0; index < 3; index ++)
    {
        //移除容器首元素。若容器中无元素则行为未定义。
        forward_list2.pop_front();
        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 << "swap before: " << std::endl;
    std::forward_list<Cell> forward_list3(6);
    std::generate(forward_list3.begin(), forward_list3.end(), generate);
    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(6);
    std::generate(forward_list4.begin(), forward_list4.end(), generate);
    std::cout << "forward_list4:    ";
    std::copy(forward_list4.begin(), forward_list4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。
    forward_list3.swap(forward_list4);

    std::cout << "swap 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;

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

    return 0;
}

输出

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: c++