当前位置: 首页 > news >正文

每日学习笔记:C++ STL 的forward_list

定义

特点

 

操作函数

 

 

元素查找、移除或安插

forward_list::emplace_after

arg...指的是元素构造函数的参数(0~N个)

#include <iostream>
#include <memory>
#include <list>
#include <forward_list>
using namespace std;class aaa
{
public:aaa() { c = 666;};aaa(int a, int b) {c = a + b;};friend std::ostream& operator << (std::ostream& o, const aaa& s) {o << s.c;return o;}private:int c;
};int main()
{forward_list<aaa> t1{ {1,1},{},{2,2} }; std::copy(t1.cbegin(), t1.cend(), std::ostream_iterator<aaa>(cout, " "));cout << endl;t1.emplace_after(t1.before_begin(), 6, 8);std::copy(t1.cbegin(), t1.cend(), std::ostream_iterator<aaa>(cout, " "));cout << endl;t1.emplace_after(t1.before_begin());std::copy(t1.cbegin(), t1.cend(), std::ostream_iterator<aaa>(cout, " "));cout << endl;forward_list<int> t{ 1,2,3 }; t.emplace_after(t.before_begin(), 55);std::copy(t.cbegin(), t.cend(), std::ostream_iterator<int>(cout, "-"));for (;;);
}

forward_list::splice_after

<C++标准库第二版>书中介绍了c.splice_after(pos,c2,c2pos)的定义【c2pos所指的下一个转移到pos所指的下一个】,但是示例代码却写成了

这个调用的参数顺序与函数定义的参数顺序并不一致,于是我专门验证了一下,发现这个写法的运行结果与按照函数定义的参数顺序t2.splice_after(pos2, t1, pos1);运行结果居然相同,甚至我改成了下面这样的调用,结果仍然相同

forward_list<int> t3; //定义一个空的forward_list
t3.splice_after(pos2, forward_list<int>(), pos1); //形参传入一个空的forward_list

细看了一下 splice_after函数实现发现,在没有开启DEBUG宏的情况下,其内部并没有操作形参传入的forward_list参数,而只是对迭代器pos2、pos1之右(后)所指内容进行了拼接。即常规的单向链表拼接的指针操作。

示例代码:

#include <iostream>
#include <forward_list>
using namespace std;int main()
{forward_list<int> t1{1,2,3,4,5};forward_list<int> t2{ 97,98,99};auto pos1 = t1.before_begin();for (auto pb1 = t1.begin(); pb1 != t1.end(); ++pb1,++pos1){if (*pb1 == 3){break;}}auto pos2 = t2.before_begin();for (auto pb2 = t2.begin(); pb2 != t2.end(); ++pb2, ++pos2){if (*pb2 == 99){break;}}t1.splice_after(pos2, t2, pos1);//t2.splice_after(pos2, t1, pos1);//forward_list<int> t3;//t3.splice_after(pos2, forward_list<int>(), pos1);//t2.splice_after(pos2, t1, t1.before_begin());//t2.splice_after(pos2, t1, t1.begin());for (;;);
}

http://www.lryc.cn/news/317504.html

相关文章:

  • 【Java,Redis】Redis 数据库存取字符串数据以及类数据
  • OpenCV 图像重映射函数remap()实例详解
  • Python基础课堂最后一课23——正则对象
  • 【算法训练营】凸包,图(Python实现)
  • webpack5零基础入门-6webpack处理图片资源
  • 计算机基础知识QA
  • 微信小程序一次性订阅requestSubscribeMessage授权和操作详解
  • ARM 汇编指令:(三)运算处理指令
  • 【C++庖丁解牛】STL简介 | string容器初次见面
  • 记OnlyOffice的两个大坑
  • 分享几个Google Chrome谷歌浏览器历史版本下载网站
  • 备考2025年AMC8竞赛:吃透2000-2024年600道真题(免费赠送真题)
  • 考研复试C语言篇
  • Docker架构深度解析:守护进程、客户端与存储驱动的协同作战(下)
  • 【强化学习笔记一】初识强化学习(定义、应用、分类、性能指标、小车上山案例及代码)
  • 安卓面试准备汇总
  • C#+datax实现定时增量同步
  • VUE实现Provide的计算属性
  • Spring Schedule:Spring boot整合Spring Schedule实战讲解定时发送邮件的功能
  • Midjourney绘图欣赏系列(十)
  • 【C语言】人生重开模拟器
  • 船舶AIS监控网络-船位信息查询:实时查询船舶动态,服务于船舶安全航行管理、港口调度计划、物流、船代、货代。【AIS动态信息编写船舶轨迹】
  • Axios 中的文件上传(Upload File)方法
  • 机试:数塔路径
  • ROS2中launch编写及参数含义(xml、python)
  • 鸿蒙Socket通信示例(TCP通信)
  • yolov5-v6.0详细解读
  • FPGA - 单总线协议(one-wire)
  • python的函数与类的定义
  • Parade Series - WebRTC ( < 300 ms Low Latency ) T.B.D