【C++STL基础入门】list交换、翻转,排序、合并和拼接操作

news2024/10/7 18:31:54

文章目录

  • 前言
  • 一、交换list
  • 二、翻转list
  • 三、排序list
  • 四、合并list
  • 五、拼接list
  • 总结


前言

在C++的标准模板库(STL)中,list是一个双向链表容器,提供了丰富的功能和操作。本文将介绍list容器在交换、翻转、排序、合并和拼接等方面的基础操作和用法。


一、交换list

1、void swap(list& from)

函数原型:void swap(list& from)

功能:将当前list与另一个list进行交换,两个list的内容互换。
参数:要交换的list

示例代码:

#include <iostream>
#include <list>

int main() {
  std::list<int> list1 = {1, 2, 3};
  std::list<int> list2 = {4, 5, 6};

  std::cout << "Before swapping:\n";
  for (const auto& num : list1) {
    std::cout << num << ' ';
  }
  std::cout << '\n';
  for (const auto& num : list2) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  list1.swap(list2);

  std::cout << "After swapping:\n";
  for (const auto& num : list1) {
    std::cout << num << ' ';
  }
  std::cout << '\n';
  for (const auto& num : list2) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  return 0;
}

在这里插入图片描述

输出结果:

Before swapping:
1 2 3
4 5 6
After swapping:
4 5 6
1 2 3

二、翻转list

1、reserve
函数原型:void reverse()

功能:将list中的元素进行翻转,即按逆序重新排列。

示例代码:

#include <iostream>
#include <list>

int main() {
  std::list<int> mylist = {1, 2, 3, 4, 5};

  std::cout << "Before reversing:\n";
  for (const auto& num : mylist) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  mylist.reverse();

  std::cout << "After reversing:\n";
  for (const auto& num : mylist) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  return 0;
}

在这里插入图片描述

输出结果:

Before reversing:
1 2 3 4 5
After reversing:
5 4 3 2 1

三、排序list

1、sort()
函数原型:void sort()

功能:按升序对list中的元素进行排序。

示例代码:

#include <iostream>
#include <list>

int main() {
  std::list<int> mylist = {5, 2, 1, 4, 3};

  std::cout << "Before sorting:\n";
  for (const auto& num : mylist) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  mylist.sort();

  std::cout << "After sorting:\n";
  for (const auto& num : mylist) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  return 0;
}

在这里插入图片描述

输出结果:

Before sorting:
5 2 1 4 3
After sorting:
1 2 3 4 5

四、合并list

1、void merge( list &lst );
函数原型:void merge(list& lst)

功能:将另一个list lst 合并到当前list中,合并后list中的元素仍然保持有序。

示例代码:

#include <iostream>
#include <list>

int main() {
  std::list<int> list1 = {1, 3, 5};
  std::list<int> list2 = {2, 4, 6};

  std::cout << "List1:\n";
  for (const auto& num : list1) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  std::cout << "List2:\n";
  for (const auto& num : list2) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  list1.merge(list2);

  std::cout << "Merged List:\n";
  for (const auto& num : list1) {
    std::cout << num << ' ';
  }
  std::cout << '\n';

  return 0;
}

在这里插入图片描述

输出结果:

List1:
1 3 5
List2:
2 4 6
Merged List:
1 2 3 4 5 6

五、拼接list

1、void splice( iterator pos, list &lst );
void splice(iterator pos, list& lst):

函数功能:将列表 lst 中的所有元素插入到目标列表 list 的 pos 位置之前, lst 中的元素会从 lst 中移除。
参数:
pos:目标列表中的插入位置的迭代器。
lst:要插入到目标列表中的源列表。
示例代码:

std::list<int> destList = {1, 2, 3};
std::list<int> srcList = {4, 5, 6};

destList.splice(destList.begin(), srcList);

// 输出目标列表
for (const auto& element : destList) {
    std::cout << element << " ";  // 输出:4 5 6 1 2 3
}

// 输出源列表
for (const auto& element : srcList) {
    std::cout << element << " ";  // 输出:(空)
}

在这里插入图片描述

2、void splice( iterator pos, list &lst, iterator del );
函数功能:将 lst 中的元素 del 插入到目标列表 list 的 pos 位置之前, del 本身会从 lst 中移除。
参数:
pos:目标列表中的插入位置的迭代器。
lst:要插入到目标列表中的源列表。
del:lst 中要移除并插入到目标列表的元素的迭代器。
示例代码:

std::list<int> destList = {1, 2, 3};
std::list<int> srcList = {4, 5, 6};

auto srcIter = std::find(srcList.begin(), srcList.end(), 5);
destList.splice(destList.begin(), srcList, srcIter);

// 输出目标列表
for (const auto& element : destList) {
    std::cout << element << " ";  // 输出:5 1 2 3
}

// 输出源列表
for (const auto& element : srcList) {
    std::cout << element << " ";  // 输出:4 6
}

在这里插入图片描述

3、void splice( iterator pos, list &lst, iterator start, iterator end );
函数功能:将源列表 lst 中从 start 到 end(不包括 end)之间的元素插入到目标列表 list 的 pos 位置之前,这些元素会从 lst 中移除。
参数:
pos:目标列表中的插入位置的迭代器。
lst:要插入到目标列表中的源列表。
start:源列表中要移除并插入到目标列表的起始位置的迭代器。
end:源列表中要移除但不插入到目标列表的结束位置的迭代器。
示例代码:

std::list<int> destList = {1, 2, 3};
std::list<int> srcList = {4, 5, 6};

auto srcStart = std::find(srcList.begin(), srcList.end(), 4);
auto srcEnd = std::find(srcList.begin(), srcList.end(), 6);
destList.splice(destList.begin(), srcList, srcStart, srcEnd);

// 输出目标列表
for (const auto& element : destList) {
    std::cout << element << " ";  // 输出:4 5 1 2 3
}

// 输出源列表
for (const auto& element : srcList) {
    std::cout << element << " ";  // 输出:6
}

在这里插入图片描述


总结

通过本文的介绍,我们了解了list容器在交换、翻转、排序、合并和拼接方面的基础操作。这些功能使得list容器成为一个强大而灵活的工具,为我们的编程提供了便利。在实际应用中,我们可以根据具体需求,灵活运用这些操作,以满足不同的场景需求。希望本文对你对C++STL中list容器的基础入门有所帮助!

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

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

相关文章

Sip多按键对讲分机,洁净室专用对讲终端

Sip多按键对讲分机&#xff0c;洁净室专用对讲终端 嵌入式洁净室电话机广泛应用于手术室&#xff0c;实验室&#xff0c;制药厂车间&#xff0c;无尘车间等 环境要求高的场所&#xff0c;整机是SUS304不锈钢工艺&#xff0c;喇叭&#xff0c;按键&#xff0c;麦克风无间隙&…

三维模型3DTile格式轻量化顶点压缩主要技术方法分析

三维模型3DTile格式轻量化顶点压缩主要技术方法分析 三维模型顶点压缩是3DTile格式轻量化压缩的重要组成部分&#xff0c;能有效减小数据大小&#xff0c;提高数据处理效率。下面将详细分析几种主要的顶点压缩技术方法&#xff1a; 预测性编码&#xff1a;预测性编码也被称为差…

串口电平信号分析--一下看懂不同的串口通信信号

串口电平信号分析–一下看懂不同的串口通信信号

C#,数值计算——Primpolytest的计算方法与源程序

1 文本格式 using System; namespace Legalsoft.Truffer { public class Primpolytest { private int N { get; set; } 32; private int nfactors { get; set; } 5; private ulong[] factors { get; set; } private int[] t { get…

九日集训 LCR.190 加密运算

计算机安全专家正在开发一款高度安全的加密通信软件&#xff0c;需要在进行数据传输时对数据进行加密和解密操作。假定 dataA 和 dataB 分别为随机抽样的两次通信的数据量&#xff1a; 正数为发送量负数为接受量0 为数据遗失 请不使用四则运算符的情况下实现一个函数计算两次…

MySQL索引看这篇就够了

能简单说一下索引的分类吗&#xff1f; 例如从基本使用使用的角度来讲&#xff1a; 主键索引: InnoDB 主键是默认的索引&#xff0c;数据列不允许重复&#xff0c;不允许为 NULL&#xff0c;一个表只能有一个主键。唯一索引: 数据列不允许重复&#xff0c;允许为 NULL 值&…

Vue.js2+Cesium1.103.0 十二、绑定多个 DOM 弹窗,并跟随视角实时更新位置

Vue.js2Cesium1.103.0 十二、绑定多个 DOM 弹窗&#xff0c;并跟随视角实时更新位置 Demo 基于 element-ui 的 Message 封装一个自定义弹窗&#xff0c;添加到页面中&#xff0c;并实时更新位置。 <template><divid"cesium-container"style"width: 1…

【RocketMQ】浅谈消息发送机制

【RocketMQ】浅谈消息发送机制 参考资料&#xff1a; 消息发送核心参数与工作原理详解 RocketMQ消息发送流程 RocketMQ 消息发送 原理详解 源码剖析 结合实际应用场景谈消息发送 《RocketMQ技术内幕》 文章目录 【RocketMQ】浅谈消息发送机制一、认识RocketMQ消息——Message二…

java语言对异常处理运行的初步探索(try-catch-finally)

​​​​​​异常处理机制 java中的异常处理机制使得即使程序出现异常&#xff0c;代码也能够继续执行下去而不是直接退出程序。下面我们先来简单的了解一下异常处理是怎么使用。 在引用异常处理之前&#xff0c;代码运行中存在异常会导致JVM直接中断该程序并输出异常信息&am…

stack的使用以及模拟实现

&#x1f388;个人主页:&#x1f388; :✨✨✨初阶牛✨✨✨ &#x1f43b;强烈推荐优质专栏: &#x1f354;&#x1f35f;&#x1f32f;C的世界(持续更新中) &#x1f43b;推荐专栏1: &#x1f354;&#x1f35f;&#x1f32f;C语言初阶 &#x1f43b;推荐专栏2: &#x1f354;…

活动预告|Dragonfly 与你相约 2023 KubeCon Shanghai!

KubeCon CloudNativeCon Open Source Summit China 2023&#xff0c;由 Linux 基金会、CNCF 主办&#xff0c;将在 9 月 26-28 日于上海跨国采购会展中心盛大开幕。本次峰会将聚集全球社区&#xff0c;共同探讨云原生和开源领域的前沿洞察、核心技术与最佳实践&#xff0c;会…

Java基于SpringBoot的藏区特产销售系统的研究与实现

今天为大家带来的是基于 Java SpringBootVue 的藏区特产销售系统&#xff0c;大家有兴趣的可以看一下 博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝30W,Csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ 文章目…

ruoyi(若依)接口拦截路径配置,接口访问要授权,放开授权直接访问

1.找到文件SecurityConfig.java文件&#xff0c;里面配置相应的放行路径

[计算机入门] Windows附件程序介绍(办公类)

3.13 Windows附件程序介绍(办公类) 3.13.1 写字板 Windows系统中的写字板程序是一款简单而实用的文本编辑工具&#xff0c;它被广泛应用于快速记录笔记、绘制草图和进行简单的文档编辑。以下是写字板程序的主要功能和作用&#xff1a; 文本输入和编辑&#xff1a;写字板程序允…

数据结构-----二叉树的创建和遍历

目录 前言 二叉树的链式存储结构 二叉树的遍历 1.前序遍历 2.中序遍历 3.后序遍历 二叉树的创建 创建一个新节点的函数接口 1.创建二叉树返回根节点 2.已有根节点&#xff0c;创建二叉树 3.已有数据&#xff0c;创建二叉树 前言 在此之前我们学习了二叉树的定义和储…

HTML5+CSS3小实例:脉冲波纹催眠动画特效

实例:脉冲波纹催眠动画特效 技术栈:HTML+CSS 效果: 源码: 【html】 <!DOCTYPE html> <html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta name="viewport" content=&qu…

ThingsBoard 前端项目背景图片部件开发

前言 ThingsBoard 是目前 Github 上最流行的开源物联网平台&#xff08;14.4k Star&#xff09;&#xff0c;可以实现物联网项目的快速开发、管理和扩展, 是中小微企业物联网平台的不二之选。 本文介绍如何在 ThingsBoard 前端项目中开发背景图片部件。 产品需求 最近接到产…

掌握可扩展和可维护应用程序:12-Factor应用程序开发的全面指南

1*vhxOhTuKSyuuAKu-nUQ5SA.jpeg 在今天的快节奏世界中&#xff0c;软件开发人员需要创建可扩展、可维护和适应性强的应用程序。12-Factor应用程序方法论是一组最佳实践&#xff0c;可以帮助开发人员实现这些目标。 本文深入探讨了12个因素&#xff0c;详细解释了它们的重要性以…

python 深度学习 解决遇到的报错问题5

目录 一、conda安装shapefile失败 二、conda安装osmnx失败&#xff1a;To search for alternate channels that may provide the conda package yourelooking for, navigate to 三、ERROR: Could not build wheels for llvmlite, which is required to install pyproject.to…

算法基础--位运算

一、常见位运算总结&#xff1a; 1、基础位运算&#xff08;^&#xff09; 其中异或^有2种理解。 2、位图bitset相关&#xff08;&|&#xff09; test判断第x位是1函数0: 可以让n右移&#xff0c;也可以让1左移&#xff0c;习惯上选择第一种 (n>>x)&1 判…