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>::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;
}

输出

 

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