list容器
list基本概念
功能:将数据进行链式存储
链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
链表的组成:链表由一系列结点组成
结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域
STL中的链表是一个双向循环链表
链表优点:
- 可以对任意位置进行插入和删除元素
链表缺点:
- 容器遍历速度没有数组快
- 占用空间比数组大
链表存储方式不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
list优点:
- 动态存储分配,不会造成内存浪费和溢出
- 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
list缺点:
- 链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大
List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。
总结:STL中List和vector是两个最常被使用的容器,各有优缺点
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);//遍历容器 10 20 30 40
list<int>L2(L1.begin(), L1.end());//区间方式构造
printList(L2);//10 20 30 40
list<int>L3(L2);//拷贝构造
printList(L3);//10 20 30 40
list<int>L4(10, 1000);//n个elem构造
printList(L4);//1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
}
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);//10 20 30 40
list<int>L2;
L2 = L1;//operator= 赋值
printList(L2);//10 20 30 40
list<int>L3;
L3.assign(L2.begin(), L2.end());
printList(L3);//10 20 30 40
list<int>L4;
L4.assign(10, 100);
printList(L4);//100 100 100 100 100 100 100 100 100 100
}
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);//10 20 30 40
printList(L2);//100 100 100 100 100 100 100 100 100 100
L1.swap(L2);
cout << "交换后:" << endl;
printList(L1);//100 100 100 100 100 100 100 100 100 100
printList(L2);//10 20 30 40
}
list大小操作
对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);//10 20 30 40
//判断容器是否为空
if (L1.empty()) {
cout << "L1 为空" << endl;
}
else {
cout << "L1 不为空" << endl;
cout << "L1的元素个数:" << L1.size() << endl;//L1的元素个数:4
}
//重新指定大小
//L1.resize(10);//10 20 30 40 0 0 0 0 0 0
L1.resize(10, 1000);
printList(L1);//10 20 30 40 1000 1000 1000 1000 1000 1000
L1.resize(2);
printList(L1);//10 20
}
- 判断是否为空 —— empty
- 返回元素个数 —— size
- 重新指定个数 —— resize
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);//300 200 100 10 20 30
L.pop_back();//尾删
printList(L);//300 200 100 10 20
L.pop_front();//头删
printList(L);//200 100 10 20
//insert插入
L.insert(L.begin(), 1000);
printList(L);//1000 200 100 10 20
list<int>::iterator it = L.begin();
L.insert(++it, 10000);
printList(L);//1000 10000 200 100 10 20
//删除
it = L.begin();//重新指定迭代器位置至起始位置
//L.erase(it);//10000 200 100 10 20
L.erase(++it);
printList(L);//1000 200 100 10 20
//移除
L.push_back(1111);
L.push_back(1111);
L.push_back(1111);
L.push_back(1111);
printList(L);//1000 200 100 10 20 1111 1111 1111 1111
L.remove(1111);
printList(L);//1000 200 100 10 20
//清空
L.clear();
printList(L);//
}
- 尾插 —— push_back
- 尾删 —— pop_back
- 头插 —— push_front
- 头删 —— pop_front
- 插入 —— insert
- 删除 —— erase
- 移除 —— remove
- 清空 —— clear
list数据存取
void test01() {
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;//第一个元素:10
cout << "最后一个元素:" << L1.back() << endl;//最后一个元素:40
//验证迭代器是不支持随机访问的
list<int>::iterator it = L1.begin();
it++;//支持双向
it--;
//it = it + 1;//出错,不支持随机访问
}
list容器中不可以通过[]或者at方式访问数据
返回第一个元素 —— front
返回最后一个元素 —— back
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 v1,int v2) {
//降序 就让第一个数 > 第二个数
return v1 > v2;
}
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);//20 10 50 40 30
//反转
L1.reverse();
cout << "反转后:" << endl;
printList(L1);//30 40 50 10 20
cout << "排序前:" << endl;
printList(L1);//30 40 50 10 20
//所有不支持随机访问迭代器的容器,不可以用标准算法
//不支持随机访问迭代器的容器,内部会提供对应一些算法
//sort(L1.begin(), L1.end());
L1.sort();//默认从小到大
cout << "排序后:" << endl;
printList(L1);//10 20 30 40 50
L1.sort(myCompare);//降序
printList(L1);//50 40 30 20 10
}
- 反转 —— reverse
- 排序 —— sort(成员函数)
排序案例
描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序
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 test01() {
list<Person>L;
//准备数据
Person p1("小八", 35, 175);
Person p2("小五", 45, 180);
Person p3("小二", 25, 171);
Person p4("小九", 40, 185);
Person p5("小六", 35, 182);
Person p6("小七", 35, 170);
//插入数据
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;
}
}
- 对于自定义数据类型,必须要指定排序规则,否则编译器不知道该如何排序
- 高级排序只是在排序规则上再进行一次逻辑规则制定
set/multiset容器
set基本概念
简介:所有元素都会在插入时自动被排序
本质:
set/multiset属于关联式容器,底层结构是用二叉树实现。
set 和 multiset区别:
- set 不允许容器中有重复的元素
- multiset 允许容器中有重复的元素
set构造和赋值
构造:
赋值:
#include<set>;
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int>s1;
//插入数据,只有insert方式
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(30);
//遍历容器
//set容器特点:所有元素插入时自动被排序
//set容器不允许插入重复值
printSet(s1);//10 20 30 40
//拷贝构造
set<int>s2(s1);
printSet(s2);//10 20 30 40
set<int>s3;//赋值
s3 = s2;
printSet(s3);//10 20 30 40
}
- multiset 头文件也是#include<set>;
- set插入数据时用insert
- set插入数据的数据会自动排序
set大小和交换
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(40);
s1.insert(20);
printSet(s1);//10 20 30 40
if (s1.empty()) {
cout << "s1 为空" << endl;
}
else {
cout << "s1 不为空" << endl;
cout << "s1的元素个数:" << s1.size() << endl;//s1的元素个数:4
}
set<int>s2;
s2.insert(100);
s2.insert(300);
s2.insert(400);
s2.insert(200);
cout << "交换前:" << endl;
printSet(s1);//10 20 30 40
printSet(s2);//100 200 300 400
cout << "交换后:" << endl;
s1.swap(s2);
printSet(s1);//100 200 300 400
printSet(s2);//10 20 30 40
}
set不支持resize(重新指定容器大小)
set插入和删除
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
printSet(s1);//10 20 30 40
s1.erase(s1.begin());//删除第一个数据
printSet(s1);//20 30 40
s1.erase(30);//删除重载版本
printSet(s1);//20 40
//清空
//s1.erase(s1.begin(), s1.end());
s1.clear();//
printSet(s1);
}
set查找和统计
void test01() {
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(30);
s1.insert(20);
//查找
set<int>::iterator pos = s1.find(30);
if (pos != s1.end()) {
cout << "找到元素:" << *pos << endl;
}
else {
cout << "未找到元素:" << endl;
}
//统计
int num = s1.count(30);
//对于set而言,统计结果只会是 0 或 1 ,因为set不允许有重复数据
cout << "num = " << num << endl;//num = 1
}
set 和 multiset 区别
区别:
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
void test01() {
set<int>s;
pair<set<int>::iterator, bool>ret = s.insert(10);
if (ret.second) {
cout << "第一次插入成功" << endl;//第一次插入成功
}
else {
cout << "第一次插入失败" << endl;
}
ret = s.insert(10);
if (ret.second) {
cout << "第二次插入成功" << endl;
}
else {
cout << "第二次插入失败" << endl;//第二次插入失败
}
multiset<int>ms;
ms.insert(10);
ms.insert(10);
ms.insert(10);
ms.insert(10);//允许插入重复值
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
cout << *it << " ";//10 10 10 10
}
cout << endl;
}
pair对组创建
成对出现的数据,利用对组可以返回两个数据
两种创建方式:
void test01() {
pair<string, int>p("Tom", 20);//第一种方式
cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
pair<string, int>p2 = make_pair("Jerry", 30);//第二种方式
cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}
set容器排序
set容器默认排序是从小到大,如何改变排序规则
主要技术点:利用仿函数
示例一:set存放内置数据类型,利用仿函数指定排序规则
class MyCompare {
public:
bool operator()(int v1,int v2) const{
return v1 > v2;
}
};
void test01() {
//默认规则 从小到大
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(50);
s1.insert(30);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
cout << *it << " ";//10 20 30 40 50
}
cout << endl;
//制定规则 从大到小
set<int, MyCompare>s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(50);
s2.insert(30);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
cout << *it << " ";//50 40 30 20 10
}
cout << endl;
}
示例二:set存放自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class mycomparePerson {
public:
bool operator()(const Person& p1, const Person& p2) const{
return p1.m_Age > p2.m_Age;//按照年龄降序
}
};
void test01() {
//自定义的数据类型,都会指定排序规则
set<Person, mycomparePerson>s;
Person p1("刘备", 24);
Person p2("关羽", 28);
Person p3("张飞", 25);
Person p4("赵云", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, mycomparePerson>::iterator it = s.begin(); it != s.end(); it++) {
cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
}
//姓名:关羽 年龄:28
//姓名:张飞 年龄:25
//姓名:刘备 年龄:24
//姓名:赵云 年龄:21
}