当前位置:网站首页>第十二章 STL 之 list
第十二章 STL 之 list
2022-07-17 10:23:00 【qq_43205256】
1 list基本概念
2 list构造函数
#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 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(L1.begin(),L1.end());
printList(L2);
list<int>L3(L2);
printList(L3);
list<int>L4(10, 1000);
printList(L4);
}
int main()
{
test01();
return 0;
}
输出结果:
10 20 30 40
10 20 30 40
10 20 30 40
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
3 list 赋值和交换
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(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, 100);
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, 100);
cout << "交换前: " << endl;
printList(L1);
printList(L2);
cout << endl;
L1.swap(L2);
cout << "交换后: " << endl;
printList(L1);
printList(L2);
}
int main()
{
test01();
test02();
return 0;
}
10 20 30 40
10 20 30 40
10 20 30 40
100 100 100 100 100 100 100 100 100 100
交换前:
10 20 30 40
100 100 100 100 100 100 100 100 100 100
交换后:
100 100 100 100 100 100 100 100 100 100
10 20 30 40
总结:list赋值和交换操作能够灵活运用即可
4 list 大小操作
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(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
if (L1.empty())
{
cout << "L1为空" << endl;
}
else
{
cout << "L1不为空" << endl;
cout << "L1的大小为: " << L1.size() << endl;
}
//重新指定大小
L1.resize(10);
printList(L1);
L1.resize(2);
printList(L1);
}
int main()
{
test01();
return 0;
}
输出结果:
L1不为空
L1的大小为: 4
10 20 30 40 0 0 0 0 0 0
10 20
5 list 插入和删除
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> 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);
printList(L);
//尾删
L.pop_back();
printList(L);
//头删
L.pop_front();
printList(L);
//插入
list<int>::iterator it = L.begin();
L.insert(++it, 1000);
printList(L);
//删除
it = L.begin();
L.erase(++it);
printList(L);
//移除
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()
{
test01();
return 0;
}
输出结果:
300 200 100 10 20 30
300 200 100 10 20
200 100 10 20
200 1000 100 10 20
200 100 10 20
200 100 10 20 10000 10000 10000
200 100 10 20
6 list 数据存取
示例:
//数据存取
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
//cout << L1.at(0) << endl;//错误 不支持at访问数据
//cout << L1[0] << endl; //错误 不支持[]方式访问数据
cout << "第一个元素为: " << L1.front() << endl;
cout << "最后一个元素为: " << L1.back() << endl;
//list容器的迭代器是双向迭代器,不支持随机访问
list<int>::iterator it = L1.begin();
//it = it + 1;//错误,不可以跳跃访问,即使是+1
}
int main()
{
test01();
return 0;
}
输出结果:
第一个元素为: 10
最后一个元素为: 40
7 list 反转和排序
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
bool myCompare(int val1, int val2)
{
return val1 > val2;
}
//反转和排序
void test01()
{
list<int> L;
L.push_back(90);
L.push_back(30);
L.push_back(20);
L.push_back(70);
printList(L);
//反转容器的元素
L.reverse();
printList(L);
//排序
L.sort();
//默认的排序规则 从小到大
printList(L);
L.sort(myCompare);
//指定规则,从大到小
printList(L);
}
int main()
{
test01();
return 0;
}
输出结果:
90 30 20 70
70 20 30 90
20 30 70 90
90 70 30 20
8 排序案例
#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();
return 0;
}
输出结果:
姓名: 刘备 年龄: 35 身高: 175
姓名: 曹操 年龄: 45 身高: 180
姓名: 孙权 年龄: 40 身高: 170
姓名: 赵云 年龄: 25 身高: 190
姓名: 张飞 年龄: 35 身高: 160
姓名: 关羽 年龄: 35 身高: 200
---------------------------------
姓名: 赵云 年龄: 25 身高: 190
姓名: 关羽 年龄: 35 身高: 200
姓名: 刘备 年龄: 35 身高: 175
姓名: 张飞 年龄: 35 身高: 160
姓名: 孙权 年龄: 40 身高: 170
姓名: 曹操 年龄: 45 身高: 180
边栏推荐
- uniapp仓库管理系统源码
- OLED显示如何理解 12*6、16*8、24*12等字符大小
- SharePoint access brief notes
- 开发实用工具
- [Network Research Institute] the threat of machine learning system is time to take it seriously
- Daily model series: July 11, 2022
- 开发第一个Flink应用
- Case sharing | build a one-stop data development platform for hehe information based on linkis+dss
- 目标检测模型大小计算,模型复杂度(参数换算公式)
- C语言力扣第25题之k个一组反转链表。多指针遍历
猜你喜欢
el-table 列拖拽(无须引入其他插件)
Markdown (5): anchor link
OLED显示如何理解 12*6、16*8、24*12等字符大小
第一部分—C语言基础篇_3. 运算符与表达式
Microservice splitting for stand-alone projects
第一部分—C语言基础篇_1. C语言概述
SAP Fiori 的附件处理(Attachment handling)
在Pycharm里面如何避免全局索引?如何取消对于某个文件夹的索引?
C语言基础篇 —— 2-3 指针与数组
Resolve the applicationeventmulticast not initialized - call 'refresh' before multicast events exception
随机推荐
【C语言】 数据类型及意义
Code Capriccio: question skimming record (under update)
岚图梦想家的产品力到底如何?
v-mode
How is MySQL data stored on disk?
codeforces每日5题(均1500)-第十七天
Questions d'entrevue - concevoir des cas d'essai pour:: memcpy
Day 5 training
The inflection point of eth may be just around the corner, which is how to
DuiLib 实现tooltip自定义鼠标提示窗口
置顶随笔分类
565. Array nesting
Daily model series: July 11, 2022
How to synchronize historical data when MySQL is upgraded to primary and standby?
ETH的拐点可能指日可待,这就是如何
MySQL view
es概念模型与基本故障
Codeworks 5 questions per day (average 1500) - day 17
Yanrong technology was selected as Beijing's "specialized and innovative" in 2022 to lead hybrid cloud file storage
mysqldump全备恢复到新另一个实例然后再执行flush privileges的情况分析