STL——list容器

news2024/10/7 4:34:02

目录

1.list基本概念

2.list构造函数

 3.list赋值和交换

4.list大小操作

5.list插入和删除

 6.list数据存取

7.list反转和排序

8.排序案例


1.list基本概念

功能:将数据进行链式存储。

链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。

链表的组成:链表由一系列结点组成。

结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域

STL中的链表是一个双向循环链表

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。 

list的优点

  • 采用动态存储分配,不会造成内存浪费和溢出
  • 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素

list的缺点

  • 链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大 

注:list有一个重要的性质,插入和删除操作都不会造成原有的list迭代器失效,这在vector容器是不成立的。 

2.list构造函数

函数原型:

  • list<T> lst; ——//list采用采用模板类实现,对象的默认构造形式。
  • list(beg,end); ——//构造函数将[beg, end)区间中的元素拷贝给本身。
  • list(n,elem); ——//构造函数将n个elem拷贝给本身。
  • list(const list &lst); ——//拷贝构造函数。
#include<iostream>
using namespace std;
#include<list>
void printList(list<int>&l)
{
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//list构造函数
void test01()
{
	//创建list容器
	list<int>l1;//默认构造
	//添加数据
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);
	//遍历容器
	printList(l1);
	//区间方式构造
	list<int>l2(l1.begin(), l1.end());
	printList(l2);
	//拷贝构造
	list<int>l3(l2);
	printList(l3);
	//n个elem
	list<int>l4(10, 1000);
	printList(l4);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 3.list赋值和交换

函数原型:

  • assign(beg, end); ——//将[beg, end)区间中的数据拷贝赋值给本身。
  • assign(n, elem); ——//将n个elem拷贝赋值给本身。
  • list& operator=(const list &lst);—— //重载等号操作符
  • swap(lst);—— //将lst与本身的元素互换。
#include<iostream>
using namespace std;
#include<list>
void printList(list<int>&l)
{
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//list——赋值和交换
//赋值
void test01()
{
	list<int>l1;
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);
	printList(l1);
	list<int>l2;
	l2 = l1;
	printList(l2);
	list<int>l3;
	l3.assign(l2.begin(), l2.end());
	printList(l3);
	list<int>l4;
	l4.assign(10, 1000);
	printList(l4);
}
//交换
void test02()
{
	list<int>l1;
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);
	list<int>l2;
	l2.assign(10, 1000);
	cout << "交换前:" << endl;
	printList(l1);
	printList(l2);
	l1.swap(l2);
	cout << "交换后:" << endl;
	printList(l1);
	printList(l2);
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

4.list大小操作

函数原型:

  • size(); ——//返回容器中元素的个数
  • empty();—— //判断容器是否为空
  • resize(num);—— //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
  • resize(num, elem); ——//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
#include<iostream>
using namespace std;
#include<list>
void printList(list<int>&l)
{
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//list容器大小操作
void test01()
{
	list<int>l1;
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);
	printList(l1);
	//判断容器是否为空
	if (l1.empty())
	{
		cout << "l1为空。" << endl;
	}
	else
	{
		cout << "l1不为空。" << endl;
		cout << "l1中元素的个数为:" << l1.size() << endl;
	}
	//重新指定大小
	l1.resize(10, 132);
	printList(l1);
	l1.resize(2);
	printList(l1);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

5.list插入和删除

函数原理:

  • push_back(elem);//在容器尾部加入一个元素
  • pop_back();//删除容器中最后一个元素
  • push_front(elem);//在容器开头插入一个元素
  • pop_front();//从容器开头移除第一个元素
  • insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
  • insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
  • insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
  • clear();//移除容器的所有数据
  • erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
  • erase(pos);//删除pos位置的数据,返回下一个数据的位置。
  • remove(elem);//删除容器中所有与elem值匹配的元素。(移除
#include<iostream>
using namespace std;
#include<list>
void printList(list<int>&l)
{
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//list容器——插入和删除
void test01()
{
	list<int>l1;
	//尾插
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	//头插
	l1.push_front(100);
	l1.push_front(200);
	l1.push_front(300);
	l1.push_front(400);
	printList(l1);
	//尾删
	l1.pop_back();
	printList(l1);
	//头删
	l1.pop_front();
	printList(l1);
	//insert——插入
	list<int>::iterator it = l1.begin();
	++it;
	// 300 1000 200 100 10 20 
	l1.insert(it, 1000);
	printList(l1);
	//erase——删除
	it = l1.begin();
	l1.erase(it);
	printList(l1);
	//remove——移除
	l1.remove(10);
	printList(l1);
	//clear——清空
	l1.clear();
	printList(l1);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 6.list数据存取

函数原型:

  • front();—— //返回第一个元素。
  • back(); ——//返回最后一个元素。
#include<iostream>
using namespace std;
#include<list>
//list容器——数据存取
void test01()
{
	list<int>l1;
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);
	cout << "第一个元素为:" << l1.front() << endl;
	cout << "最后一个元素为:" << l1.back() << endl;
	//验证迭代器是不支持随机访问的
	list<int>::iterator it = l1.begin();
	it++;//支持双向
	it--;
	//it = it + 1;//不支持随机访问
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 注:list容器不可以通过[]或者at方式访问数据。

7.list反转和排序

函数原型:

  • reverse(); ——//反转链表
  • sort(); ——//链表排序
#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
void printList(const list<int>&l)
{
	for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//list容器——反转和排序
//反转
void test01()
{
	list<int>l1;
	l1.push_back(50);
	l1.push_back(20);
	l1.push_back(60);
	l1.push_back(80);
	l1.push_back(70);
	l1.push_back(40);
	cout << "反转前:";
	printList(l1);
	//反转
	cout << "反转后:";
	l1.reverse();
	printList(l1);
}
bool myCompare(int v1, int v2)
{
	//降序   就让第一个数>第二个数
	return v1 > v2;
}
//排序
void test02()
{
	list<int>l1;
	l1.push_back(50);
	l1.push_back(20);
	l1.push_back(60);
	l1.push_back(80);
	l1.push_back(70);
	l1.push_back(40);
	cout << "排序前:";
	printList(l1);
	//sort(l1.begin(), l1.end());//标准算法
	//所有不支持随机访问的迭代器的容器,不可用标准算法
	//不支持随机访问迭代器的容器,内部会提供对应的一些算法
	l1.sort();//默认从小到大——升序
	cout << "排序后:";
	printList(l1);
	l1.sort(myCompare);//降序
	printList(l1);
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

注:想要实现list容器的降序排序需要用sort(成员函数) 。

8.排序案例

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高。

排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序。

#include<iostream>
using namespace std;
#include <list>
#include <string>
class Person {
public:
	Person(string name, int age, int height) {
		m_Name = name;
		m_Age = age;
		m_Height = height;
	}
public:
	string m_Name; //姓名
	int m_Age; //年龄
	int m_Height; //身高
};
bool ComparePerson(Person& p1, Person& p2) {
	if (p1.m_Age == p2.m_Age) {
		return p1.m_Height > p2.m_Height;
	}
	else
	{
		return p1.m_Age < p2.m_Age;
	}
}
void test01() {
	list<Person> L;
	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("张飞", 35, 160);
	Person p6("关羽", 35, 200);
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	L.push_back(p6);
	for (list<Person>::iterator it = L.begin(); it != L.end(); it++) {
		cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age
			<< " 身高: " << it->m_Height << endl;
	}
	cout << "---------------------------------" << endl;
	L.sort(ComparePerson); //排序
	for (list<Person>::iterator it = L.begin(); it != L.end(); it++) {
		cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age
			<< " 身高: " << it->m_Height << endl;
	}
}
int main() {
	test01();
	system("pause");
	return 0;
}

注:

  • 对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序。
  • 高级排序只是在排序规则上再进行一次逻辑规则制定。

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

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

相关文章

2023-12-29 服务器开发-Centos部署LNMP环境

摘要: 2023-12-29 服务器开发-Centos部署LNMP环境 centos7.2搭建LNMP具体步骤 1.配置防火墙 CentOS 7.0以上的系统默认使用的是firewall作为防火墙&#xff0c; 关闭firewall&#xff1a; systemctl stop firewalld.service #停止firewall systemctl disable fire…

redis 三主六从高可用docker(不固定ip)

redis集群(cluster)笔记 redis 三主三从高可用集群docker swarm redis 三主六从高可用docker(不固定ip) 此博客解决&#xff0c;redis加入集群后&#xff0c;是用于停掉后重启&#xff0c;将nodes.conf中的旧的Ip替换为新的IP&#xff0c;从而达到不会因为IP变化导致集群无法…

设计模式-多例模式

设计模式专栏 模式介绍多例模式和单例模式的区别应用场景Spring中多例模式的优缺点代码示例Java实现多例模式Python实现多例模式 多例模式在spring中的应用 模式介绍 多例模式是一种创建型设计模式&#xff0c;属于对象创建类型。多例模式的特点是允许一个类有多个实例&#x…

在Adobe Acrobat上如何做PDF文档签名

Adobe Acrobat如何做PDF文档签名&#xff1f;PDF文档签名是指对PDF文档进行基于证书的数字签名&#xff0c;类似于传统的手写签名&#xff0c;可标识签名文档的人员。与手写签名不同&#xff0c;数字签名难以伪造&#xff0c;因为其包含签名者唯一的加密信息。为PDF文档进行基于…

【计算机视觉】角点检测(Harris、SIFT)

Harris 角点指的是窗口延任意方向移动&#xff0c;都有很大变化量的点。 用数学公式表示为&#xff1a; E(u,v)反映的移动后窗口的差异&#xff0c;w(x,y)为每个像素的点权值&#xff0c;I(xu,yv)是移动的像素值&#xff0c;I(x,y)是移动前的像素值。 将E(u,v)进行泰勒展开&am…

MVC : python实现

不得不承认python是一门强大的语言&#xff0c;也因此才能hold得住人工智能的逻辑实现 MVC的妙处&#xff1a;只要Model的数据结构内容和结构明确&#xff0c;Controller对抽象的View即前面明确的Model的控制逻辑是几乎不怎么改动的&#xff0c;那么。 1、任意替换View的功能已…

MetalLB:本地Kubernetes集群的LoadBalancer负载均衡利器

背景 在本地集群进行测试时&#xff0c;我们常常面临一个棘手的问题&#xff1a;Service Type不支持LoadBalancer&#xff0c;而我们只能选择使用NodePort作为替代。这种情况下&#xff0c;我们通常会配置Service为NodePort&#xff0c;并使用externalIPs将流量导入Kubernetes…

Flink实时电商数仓之DWS层

需求分析 关键词 统计关键词出现的频率 IK分词 进行分词需要引入IK分词器&#xff0c;使用它时需要引入相关的依赖。它能够将搜索的关键字按照日常的使用习惯进行拆分。比如将苹果iphone 手机&#xff0c;拆分为苹果&#xff0c;iphone, 手机。 <dependency><grou…

关于java选择结构switch及反编译

关于java选择结构switch及反编译 在上一篇文章中&#xff0c;我们了解了选择结构中的if else等&#xff0c;本章内容让我们说明一下上一篇文章中的伏笔&#xff0c;switch选择结构&#x1f914; switch多选择结构 多选择结构&#xff1a;多选择结构除了else if &#xff0c;…

Github 2023-12-29 开源项目日报 Top10

根据Github Trendings的统计&#xff0c;今日(2023-12-29统计)共有10个项目上榜。根据开发语言中项目的数量&#xff0c;汇总情况如下&#xff1a; 开发语言项目数量Java项目2HTML项目2TypeScript项目2Python项目2非开发语言项目2C项目1JavaScript项目1 精选面试问题列表 创…

Xamarin开发:商场促销(策略设计模式)

Xamarin开发:商场促销&#xff08;策略设计模式&#xff09; 一、介绍二、需求分析三、实现四、需求分析问题1解决方案问题2解决方案 五、增加新需求六、代码优化与分析总结 一、介绍 本文引用《大话设计模式》第二章节的内容进行学习分析&#xff0c;仅供学习使用 这里接着我…

centos7.9 TCP 加速

BBR是谷歌开发的新的TCP加速算法&#xff0c;在网络状况不好的服务器上开启TCP的bbr&#xff0c;可以在无需增加任何硬件投入的情况下实现网络加速&#xff0c;并且客户端无需做任何配置&#xff0c;因此使用起来非常的方便。TCP加速对网络状况较好的内网环境&#xff0c;或者大…

【数据结构和算法】找出两数组的不同

其他系列文章导航 Java基础合集数据结构与算法合集 设计模式合集 多线程合集 分布式合集 ES合集 文章目录 其他系列文章导航 文章目录 前言 一、题目描述 二、题解 2.1 哈希类算法题注意事项 2.2 方法一&#xff1a;哈希法 三、代码 3.1 方法一&#xff1a;哈希法 四…

Oracle(4)

子查询 子查询语法很简单&#xff0c;就是select 语句的嵌套使用。 查询工资比SCOTT高的员工信息 分析&#xff1a;两步即可完成 1. 查出SCOTT的工资 SQL> select ename, sal from emp where enameSCOTT 其工资3000 2. 查询比3000高的员工 SQL> select * from emp…

【零基础入门VUE】VueJS - 环境设置

✍面向读者&#xff1a;所有人 ✍所属专栏&#xff1a;零基础入门VUE专栏https://blog.csdn.net/arthas777/category_12537076.html 直接在 HTML 文件中使用 <script> 标签 <html><head><script type "text/javascript" src "vue.min.j…

Spark 集群搭建

文章目录 搭建前准备安装搭建解压并重命名环境变量配置配置文件yarn-site.xmlspark-env.sh 官网求 π(PI) 案例启动spark-shell通过浏览器查看显示查看 Spark 的网页信息展示 搭建前准备 下载地址&#xff1a;Index of /dist/spark (apache.org) 配置好 hadoop 环境&#xff…

实战 | 使用OpenCV快速去除文档中的表格线条(步骤 + 源码)

导 读 本文主要介绍如何使用OpenCV快速去除文档中的表格线条,并给详细步骤和代码。 背景介绍 测试图如下,目标是去除下面三张图中的表格线条,方便后续图像处理。 实现步骤 下面演示详细步骤,以图1为例: 【1】获取二值图像:加载图像、转为灰度图、OTSU二值化 i…

Awesome Chrome Form UI - 框架设计与基础实现

Money is not evil by itself. Its just paper with perceived value to obtain other things we value in other ways. If not money what is evil you may ask? Evil is the unquenchable, obsessive and moral bending desire for more. Evil is the bottomless,soulless …

多模态大模型-CogVLm 论文阅读笔记

多模态大模型-CogVLm 论文阅读笔记 COGVLM: VISUAL EXPERT FOR LARGE LANGUAGEMODELS 论文地址 :https://arxiv.org/pdf/2311.03079.pdfcode地址 : https://github.com/THUDM/CogVLM时间 : 2023-11机构 : zhipuai,tsinghua关键词: visual language model效果:&#xff08;2023…

C++面向对象(OOP)编程-C++11新特性详解

C11作为一个重要的版本&#xff0c;引入了很多新的特性&#xff0c;解决了C语言本身很多遗留的内存泄露问题&#xff0c;并且提供了很多比较灵活的用法。引入的auto&#xff0c;智能指针、线程机制都使得C语言的灵活性、安全性、并发性有了很大的提升。 本文会比较详细的介绍C1…