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

list(链表)容器(二)

一、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(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << (*it) << " ";}cout << endl;
}void test()
{list<int>L;//尾插L.push_back(10);L.push_back(20);L.push_back(30);//头插L.push_front(100);L.push_front(200);L.push_front(300);//300 200 100 10 20 30printList(L);//尾删	300 200 100 10 20L.pop_back();printList(L);//头删	200 100 10 20L.pop_front();printList(L);//insert插入	200 1000 100 10 20list<int>::iterator it = L.begin();L.insert(++it,1000);printList(L);//删除	200 100 10 20it = L.begin();L.erase(++it);printList(L);//移除L.push_back(10000);L.push_back(10000);L.push_back(10000);L.push_back(10000);printList(L);L.remove(10000);//删除所有printList(L);//清空L.clear();printList(L);
}int main()
{test();return 0;
}

总结:

尾插 --- push_back        尾删 --- pop_back

头插 --- push_front        头删 --- pop_front

插入 --- insert                 删除 --- erase

移除 --- remove              清空 --- clear  

二、list 数据存取

函数原型:

front(); //返回第一个元素。
back(); //返回最后一个元素。

代码示例:

#include<iostream>
using namespace std;
#include<list>void test()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//L1[0]	不可以用[]访问list容器中的元素//L1.at(0) 不可以用at访问list容器中的元素//原因是list本质为链表,不适用连续线性空间存储数据,迭代器也是不支持随机访问的cout << "第一个元素为:" << L1.front() << endl;cout << "最后一个元素为:" << L1.back() << endl;//验证迭代器是不支持随机访问的list<int>::iterator it = L1.begin();it++;//it--,支持双向,但写成it=it+1则出错,不支持随机访问
}int main()
{test();return 0;
}

总结:

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

返回第一个元素 --- front

返回最后一个元素 --- back

三、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;
}void test01()
{//反转链表list<int>L1;L1.push_back(20);L1.push_back(10);L1.push_back(50);L1.push_back(40);L1.push_back(30);cout << "反转前:" << endl;printList(L1);//反转L1.reverse();cout << "反转后:" << endl;printList(L1);
}bool myCompare(int v1,int v2)
{//降序 就让第一个数 > 第二个数return v1 > v2;
}//排序链表
void test02()
{list<int>L1;L1.push_back(20);L1.push_back(10);L1.push_back(50);L1.push_back(40);L1.push_back(30);//排序cout << "排序前:" << endl;printList(L1);//所有不支持随机访问迭代器的容器,不可以用标准算法// 不支持随机迭代器访问的容器,内部会提供一些对应算法//sort(L1.begin(), L1.end());L1.sort();//默认排序规则 从小到大 升序cout << "排序后:" << endl;printList(L1);L1.sort(myCompare);printList(L1);
}int main()
{//test01();test02();return 0;
}

总结:

反转 --- reverse

排序 --- sort (成员函数)

四、排序案例

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

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

代码示例:

#include<iostream>
using namespace std;
#include<list>
#include<string>//list容器 排序案例
class Person
{
public:Person(string name, int age, int height){this->m_Name = name;this->m_Age = age;this->m_Height = height;}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 test()
{//创建容器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<Person49>::iterator it = L.begin(); it != L.end(); it++){cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高:" << (*it).m_Height << endl;}//排序cout << "----------------------------" << 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()
{test();return 0;
}

总结:

对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序

高级排序只是在排序规则上再进行一次逻辑规则制定,并不复杂

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

相关文章:

  • 世优科技上榜2024年度《中国虚拟数字人影响力指数报告》
  • 【调试方法】C代码中dump中间数据的方法
  • 【BUG】vue中@change时间传值丢失问题
  • Linux提权!!!
  • Android Studio学习7——常用控件view
  • Springboot3 集成knife4j(swagger)
  • 深信服:借助观测云实现全链路可观测性
  • 详解Qt中使用线程
  • 在.Net6中用gdal实现第一个功能
  • 采用大语言模型进行查询重写——Query Rewriting via Large Language Models
  • 使用Vue实现CSS过渡和动画
  • 一家购物商场的数据运营挑战
  • React Native框架开发APP,安装免费的图标库(react-native-vector-icons)并使用详解
  • idea端口占用
  • MQ消息队列详解以及MQ重复消费问题
  • 系统IO函数接口
  • 06 监听器
  • C语言第三十九弹---预处理(上)
  • 计算机视觉无人驾驶技术:入门指南
  • Golang和Java对比
  • 2024.2.29力扣每日一题——统计可能的树根数目
  • 同一个主机配置多个SSH key
  • SAP系统财务模块简介:实现财务管理的卓越之道
  • 【pytest】功能特性及常用插件
  • 基于SpringBoot和Vue的房产销售系统的设计与实现
  • ROS2从入门到精通1-2:详解ROS2服务通信机制与自定义服务
  • vue两个特性和什么是MVVM
  • CAD Plant3D 2023 下载地址及安装教程
  • 集成电路企业tapeout,如何保证机台数据准确、完整、高效地采集?
  • Nginx三大常用功能“反向代理,负载均衡,动静分离”