当前位置:网站首页>第十二章 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 10003 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 205 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 206 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
最后一个元素为: 407 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 208 排序案例
#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边栏推荐
- DuiLib 实现tooltip自定义鼠标提示窗口
- 研究发现DNA纳米设备注射液可安全用于医疗用途
- 07---布儒斯特角
- Vector容器的系列操作( 详解 )
- Pyodide 中实现网络请求的 3 种方法
- Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
- 【C语言】 数据类型及意义
- 【C语言】指针练习题2——笔试真题及解析
- 力扣(LeetCode)197. 上升的温度(2022.07.16)
- 易贝按关键字搜索EBAY商品 API 返回值说明
猜你喜欢

SAP Fiori 的附件处理(Attachment handling)

Set the ID field to increase automatically when creating tables in SQL Server (Navicat demo)

第一部分—C语言基础篇_3. 运算符与表达式

Utility series - xshell installation, download and use

岚图梦想家的产品力到底如何?

日志脱敏-参考

How is MySQL data stored on disk?

将视频格式转换为gif图片格式

Markdown (5): anchor link

【C语言】指针练习题2——笔试真题及解析
随机推荐
C语言力扣第25题之k个一组反转链表。多指针遍历
SAP Fiori 的附件处理(Attachment handling)
开发第一个Flink应用
【C语言】函数知识点总结
【C语言】数组知识点总结
如何正确执行Jedis单元测试
Anycontrol demo demo demo
组件间的相互访问
MySQL升级为主备,如何同步历史数据?
Could NOT find CUDA (missing: CUDA_INCLUDE_DIRS) (found suitable exact version “11.4“)
Makefile中在命令前加上- 则忽略该命令产生的错误,继续执行下一条命令
Fundamentals of C language -- 2-2 const keywords and pointers
Uniapp warehouse management system source code
每日模型系列:2022.07.11
es概念模型与基本故障
Two structures ifconf and ifreq
Classification of top essays
【C语言】自定义类型初阶知识点
力扣(LeetCode)197. 上升的温度(2022.07.16)
置顶随笔分类