c++11 标准模板(STL)(std::forward_list)(十二)

news2024/9/22 13:42:40

定义于头文件 <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 移动元素

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

void splice_after( const_iterator pos, forward_list& other );

(1)(C++11 起)

void splice_after( const_iterator pos, forward_list&& other );

(1)(C++11 起)

void splice_after( const_iterator pos, forward_list& other,
                   const_iterator it );

(2)(C++11 起)

void splice_after( const_iterator pos, forward_list&& other,
                   const_iterator it );

(2)(C++11 起)

void splice_after( const_iterator pos, forward_list& other,
                   const_iterator first, const_iterator last );

(3)(C++11 起)

void splice_after( const_iterator pos, forward_list&& other,
                   const_iterator first, const_iterator last );

(3)(C++11 起)

 

从另一 forward_list 移动元素到 *this 。

不复制元素。 pos 必须是指向 *this 中的可解引用迭代器或 before_begin() 迭代器(特别是 end() 不是 pos 的合法参数值)。若 get_allocator() != other.get_allocator() 则行为未定义。没有迭代器或引用被非法化,指向被移动的元素的迭代器现在指代到 *this 中,而非 other 中。

1) 从 other 移动所有元素到 *this 。元素被插入到 pos 所指向的元素后。操作后 other 变为空。若 other 与 *this 指代同一对象则行为未定义。

2) 从 other 移动后随 it 的迭代器所指向的元素到 *this 。元素被插入到 pos 所指向的元素后,若 pos == it 或若 pos == ++it 则无效果。

3) 从 other 移动范围 (first, last) 中的元素到 *this 。元素被插入到 pos 所指向的元素后。不移动 first 所指向的元素。若 pos 是范围 (first,last) 中的元素则行为未定义。

参数

pos-指向将插入内容到其后的元素的迭代器
other-移动内容来源的另一容器
it-指向从 other 移动到 *this 的元素的迭代器的前趋迭代器
first, last-other 移动到 *this 的元素范围

返回值

(无)

异常

不抛出。

复杂度

1) 与 other 的大小成线性

2) 常数

3) 与 std::distance(first, last) 成线性

调用示例

#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(3);
    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;

    std::forward_list<Cell> forward_list2(3);
    std::generate(forward_list2.begin(), forward_list2.end(), generate);
    std::cout << "forward_list2:    ";
    std::copy(forward_list2.begin(), forward_list2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //1) 从 other 移动所有元素到 *this 。
    forward_list1.splice_after(forward_list1.begin(), forward_list2);
    std::cout << "splice_after:     ";
    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_list3(3);
    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(3);
    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;

    //1) 从 other 移动所有元素到 *this 。移动语义。
    forward_list3.splice_after(forward_list3.begin(), std::move(forward_list4));
    std::cout << "splice_after:     ";
    std::copy(forward_list3.begin(), forward_list3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::forward_list<Cell> forward_list5(3);
    std::generate(forward_list5.begin(), forward_list5.end(), generate);
    std::cout << "forward_list5:    ";
    std::copy(forward_list5.begin(), forward_list5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::forward_list<Cell> forward_list6(3);
    std::generate(forward_list6.begin(), forward_list6.end(), generate);
    std::cout << "forward_list6:    ";
    std::copy(forward_list6.begin(), forward_list6.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //2) 从 other 移动后随 it 的迭代器所指向的元素到 *this 。
    forward_list5.splice_after(forward_list5.begin(), forward_list6, forward_list6.before_begin());
    std::cout << "splice_after:     ";
    std::copy(forward_list5.begin(), forward_list5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::forward_list<Cell> forward_list7(3);
    std::generate(forward_list7.begin(), forward_list7.end(), generate);
    std::cout << "forward_list7:    ";
    std::copy(forward_list7.begin(), forward_list7.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::forward_list<Cell> forward_list8(3);
    std::generate(forward_list8.begin(), forward_list8.end(), generate);
    std::cout << "forward_list8:    ";
    std::copy(forward_list8.begin(), forward_list8.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //2) 从 other 移动后随 it 的迭代器所指向的元素到 *this 。 移动语义。
    forward_list7.splice_after(forward_list7.begin(), std::move(forward_list8), forward_list8.before_begin());
    std::cout << "splice_after:     ";
    std::copy(forward_list7.begin(), forward_list7.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::forward_list<Cell> forward_list9(3);
    std::generate(forward_list9.begin(), forward_list9.end(), generate);
    std::cout << "forward_list9:    ";
    std::copy(forward_list9.begin(), forward_list9.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::forward_list<Cell> forward_list10(3);
    std::generate(forward_list10.begin(), forward_list10.end(), generate);
    std::cout << "forward_list10:   ";
    std::copy(forward_list10.begin(), forward_list10.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //3) 从 other 移动范围 (first, last) 中的元素到 *this 。
    forward_list9.splice_after(forward_list9.begin(), forward_list10, forward_list10.begin(), forward_list10.end());
    std::cout << "splice_after:     ";
    std::copy(forward_list9.begin(), forward_list9.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::forward_list<Cell> forward_list11(3);
    std::generate(forward_list11.begin(), forward_list11.end(), generate);
    std::cout << "forward_list11:   ";
    std::copy(forward_list11.begin(), forward_list11.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::forward_list<Cell> forward_list12(3);
    std::generate(forward_list12.begin(), forward_list12.end(), generate);
    std::cout << "forward_list12:   ";
    std::copy(forward_list12.begin(), forward_list12.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //2) 从 other 移动后随 it 的迭代器所指向的元素到 *this 。 移动语义。
    forward_list11.splice_after(forward_list11.begin(), std::move(forward_list12), forward_list12.begin(), forward_list12.end());
    std::cout << "splice_after:     ";
    std::copy(forward_list11.begin(), forward_list11.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

输出

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/182090.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据库管理-第五十四期 春节俩故障(20230128)

数据库管理 2023-01-28第五十四期 春节俩故障1 19.13 bug 320763052 19.15 CSS总结第五十四期 春节俩故障 虽然春节期间除了年三十的现场值班和远程值班&#xff0c;没啥事的&#xff0c;结果还是处理了俩故障&#xff0c;今天上工&#xff0c;分析一下。 1 19.13 bug 320763…

了解3dmax面片建模方式

首先在模式里面选中面片栅格&#xff0c;Patch Grids&#xff1b; 选择四边形面片&#xff1b; 在顶视图中画一个面片&#xff0c;之后如下&#xff1b;面片从前和左看到的是一条线&#xff1b; 调整面片参数&#xff0c;长度分段和宽度分段分别为2和3&#xff1b; 工具栏选中修…

信息论复习—线性分组码的基本性质

目录 线性分组码&#xff1a; 非线性码示例&#xff1a; 线性码示例: 许用码字间的距离&#xff0d;&#xff0d;码距&#xff1a; 码距与码的检错纠错能力之间的关系&#xff1a; 线性分组码的基本性质&#xff1a; 线性分组码的最小码距与最小码重的关系&#xff1a; …

最详细的华为VRP操作指南

第二章&#xff1a;华为VRP系统 通用路由平台VRP&#xff08;Versatile Routing Platform&#xff09;是华为公司数据通信产品的通用操作系统平台。它以IP业务为核心&#xff0c;采用组件化的体系结构&#xff0c;在实现丰富功能特性的同时&#xff0c;还提供了基于应用的可裁…

Android深入系统完全讲解(43)

为什么要编码&#xff1f;这里主要是因为信息有甬余需要压缩&#xff0c;所以会出现各类算法。如果存储原始 数据&#xff0c;那就很大。 举例来说&#xff0c;BMP&#xff0c;压缩算法有 jpg&#xff0c;png 等等。 比如 PCM&#xff0c;压缩的 mp3 H.264 相关 这里我们看一下…

剑指 Offer 09. 用两个栈实现队列(力扣)

一&#xff1a;题目用两个栈实现一个队列。队列的声明如下&#xff0c;请实现它的两个函数 appendTail 和 deleteHead &#xff0c;分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素&#xff0c;deleteHead 操作返回 -1 )示例 1&#xff1a;输入&…

小米万兆路由器里的Docker安装Alist

小米2022年12月份发布了万兆路由器&#xff0c;里面可以使用Docker。 今天尝试在小米的万兆路由器里安装Alist v3.9.2。 准备工作 请参考https://engchina.blog.csdn.net/article/details/128515422的准备工作。 创建存储 在第三方管理(SimpleDocker)&#xff0c;单击"…

Scala-变量和数据类型

注释在Scala中注释和Java基本一样单行注释&#xff1a;// 多行注释&#xff1a;/* */ 文档注释&#xff1a;/****/变量和常量基本语法声明变量&#xff1a;var 变量名 [:变量类型] 初始值 如&#xff1a;var a:Int 10 声明常量&#xff1a;val 变量名 [:常量类型] 初始值 如…

深度卷积神经网络、池化层

目录1.深度卷积神经网络(a deep convolutional neural network)输入图像的维度是&#xff0c;如果&#xff0c;计算输出图像维度公式&#xff1a;。s表示步幅&#xff0c;p表示填充的层数。filters的通道数是和输入图像的通道数保持一致的。分析上图案例&#xff1a;第一层卷积…

《深入浅出计算机组成原理》学习笔记 Day13

数据通路&#xff08;中&#xff09;1. 时钟信号的硬件实现2. 通过 D 触发器实现存储功能参考1. 时钟信号的硬件实现 有些电路只要需要给定输入&#xff0c;就能得到固定的输出&#xff0c;这样的电路称为组合逻辑电路&#xff08;Combination Logic Circuit&#xff09;。 时…

如何使用自己的虚拟机(linux)做个RAID玩?

在虚拟机原有基础上添加两块磁盘&#xff08;均20G&#xff09;&#xff0c;步骤如下&#xff1a;使用命令“lsblk”检查添加是否生效&#xff1b;使用命令“mdadm”进行linux的raid管理&#xff08;先安装mdadm工具&#xff09;创建RAID1,名字为md1&#xff0c;即使用mdadm命令…

虚拟机字节码执行引擎

------摘自周志明 《深入理解Java虚拟机》运行时栈帧结构Java虚拟机以方法作为最基本的执行单元&#xff0c;“栈帧”&#xff08;Stack Frame&#xff09;则是用于支持虚拟机进行方法调用和方法执行背后的数据结构&#xff0c;它也是虚拟机运行时数据区中的虚拟机栈&#xff…

C语言及算法设计课程实验三:最简单的C程序设计——顺序程序设计((一、二、三、四)的综合文章)

C语言及算法设计课程实验三&#xff1a;最简单的C程序设计——顺序程序设计&#xff08;&#xff08;一、二、三、四&#xff09;的综合文章&#xff09;一、实验目的二、 实验内容2.1、实验内容1&#xff1a;通过下面的程序掌握各种格式转换符的正确使用方法2.2、实验内容2&am…

十年聚焦,巨杉数据库再获毕马威2022领先金融科技50企业殊荣

巨杉数据库凭借在金融科技与分布式数据库领域出色的市场表现与过硬的技术实力&#xff0c;成功入选“2022中国领先金融科技50企业”&#xff0c;成为本次榜单唯一入选的分布式数据库厂商。 1月16日&#xff0c;「2022毕马威中国金融科技企业双50榜单」&#xff08;下简称榜单&a…

数组扁平化,操作方法

数组扁平化是指将一个多维数组变成一维数组。 [1, [[2, 3], 4]] --> [1, 2, 3, 4] 目录 方法1&#xff1a; Array.prototype.flat() 方法2&#xff1a;Array.prototype.toString() 和 Array.prototype.join() 方法3&#xff1a;递归 方法4&#xff1a;扩展运算符 方法5…

Centos下安装ActiveMQ消息中间件

记录一下在centos7.x下面安装activeMQ消息中间件在安装ActiveMQ之前必须保证服务器安装了java环境安装java环境的地址:安装java环境找到activeMQ的官网下载安装包https://activemq.apache.org/components/classic/download/java版本是java8从官网下载后(apache-activemq-5.16.5…

【Linux】题解:生产者与消费者模型(附源代码)

【Linux】题解&#xff1a;生产者与消费者模型&#xff08;附源代码&#xff09; 摘要&#xff1a;本文主要介绍生产者与消费者模型&#xff0c;其中主要内容分为对该模型的介绍及分析&#xff0c;阻塞队列实现该模型&#xff0c;并对其升级实现多生产者多消费者并行执行。其中…

Day14 基于AOP的声明式事务控制

1 Spring 事务编程概述PlatformTransactionManager TransactionDefinition TransactionStatus2 搭建环境数据库准备一个账户表tb account;dao层准备一个AccountMapper&#xff0c;包括incrMoney和decrMoney两个方法;service层准备一个transferMoney方法&#xff0c;分别调用in…

18行列式及其性质

从此课开始&#xff0c;就进入了这门课的第二部分。迄今为止&#xff0c;已经学习了很多关于长方矩阵的知识&#xff0c;现在&#xff0c;把注意力转向方阵&#xff0c;探讨两个大的话题&#xff1a;行列式和特征值&#xff0c;需要行列式的重要原因是求特征值。 行列式是跟每…

U3772频谱分析仪

18320918653 U3772 新的便携式频谱分析仪具有体积小&#xff0c;重量轻的特点&#xff0c;可以在微波和毫米波范围内测量无线信号日本株式会社爱德万测试是全球半导体测试系统的领先企业&#xff0c;于2005年7月6日发布了一种新的便携式频谱分析仪U3771&#xff08;频率达到3…