3.7、list容器
3.7.1、queue基本概念
1、功能:
- 将数据进行链式存储
2、list(链表)是一种物理单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针连接实现的
- 链表的组成:链表由由一系列的节点组成
- 节点的组成:一个是存储数据的数据域,另一个存储下一个节点地址的指针域
- STL中的链表是一个双向循环列表
- 由于链表的存储方式并不是连续的存储空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
3、 优点:
- 采用动态存储分配,不会造成内存浪费和溢出
- 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量的元素
4、缺点:
- 链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大
5、总结:
- STL中list和vector是最常被使用的容器,各有优缺点
3.7.2、list构造函数
1、功能描述:
- 创建list容器
2、函数原型:
- list lis; //list采用模板类实现,list对象的默认构造形式
- list(beg,end); //构造函数将[beg,end)区间中的元素拷贝给本身
- list(n,elem); //构造函数将n个elem拷贝给本身
- list(const list &lis); //拷贝构造函数
3、代码
#include<iostream>
#include<list>
#include<string>
using namespace std;
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容器
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(3,6);
printList(L4);
}
int main()
{
test01();
system("pause");
return 0;
}
4、总结:
- list构造方式同其他几个STL常用容器,熟练掌握即可
5、运行结果
3.7.3、list赋值和交换
1、
- 功能描述:
- 给list容器进行赋值和交换
2、函数原型:
- assign(beg.end); //将[beg,end)区间中的数据拷贝赋值给本身
- assign(n,elem); //构造函数将n个elem拷贝给本身
- list& operator=(const list &lst); //重载等号运算符
- sawp(lis); //将lst与本身的元素交换
3、代码
#include<iostream>
#include<list>
#include<string>
using namespace std;
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容器
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.assign(L1.begin(), L1.end());
printList(L2);
//
list<int>L3;
L3.assign(3, 8);
printList(L3);
//等号重载
list<int>L4;
L4 = L2;
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(3, 8);
//交换前
cout << "交换前:" << endl;
cout << "L1为:";
printList(L1);
cout << "L2为:";
printList(L2);
//交换后
L1.swap(L2);
cout << "交换后:" << endl;
cout << "L1为:";
printList(L1);
cout << "L2为:";
printList(L2);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
4、总结:
- list赋值和交换操作能够灵活运用即可
5、运行结果
3.7.4、list大小操作
1、功能描述:
- 给list容器的大小进行操作
2、函数原型:
- empty(); //判断容器是否为空
- capacity(); //容器的容量
- size(); //返回容器中元素的个数
- resize(int num); //重新指定容器的长度为num。若容器变大,则以默认值填充新位置:
-
//如果容器变短,则末尾超出容器长度的元素被删除
- resize(int num,elem); //重新指定容器的长度为num。若容器变大,则以elem填充新位置:
-
//如果容器变短,则末尾超出容器长度的元素被删除
3、代码
#include<iostream>
#include<list>
#include<string>
using namespace std;
void printList(list<int>& lst)
{
for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
{
cout << *it << " ";
}
cout<<endl;
}
//vector赋值
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,888);//利用重载版本,可以指定默认填充值
printList(L1);
cout << "L1新大小为:" << L1.size() << endl;
L1.resize(2);//如果重新指定的比原来短了。超出部分会删除
printList(L1);
cout << "L1新大小为:" << L1.size() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4、总结:
- 判断是否为空…empty
- 返回元素个数…size
- 重新指定大小…resize
5、运行结果
3.7.5、list插入和删除
1、功能描述:
- 对list容器进行插入和删除操作
2、函数原型:
- push_back(elem); //尾部插入元素elem
- pop_back(); //删除尾部最后一个元素
- push_front(elem); //头部插入元素elem
- pop_front(); //删除头部最后一个元素
- insert(pos,elem); //在pos位置插入元素ele,返回新数据的位置
- insert(pos,n,elem); //在pos位置插入n个元素elem,无返回值
- insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值
- clear(); //删除容器中的所有元素
- erase(pos); //删除pop位置的数据,返回下一个数据的位置
- erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置
- remove(elem) //删除容器中的所有与elem值匹配的元素
3、代码
#include<iostream>
#include<list>
#include<string>
using namespace std;
void printLsit(const list<int>& lst)
{
for (list<int>::const_iterator it = lst.begin(); it != lst.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);//push_back尾部插入元素
L1.push_back(20);
L1.push_back(30);
L1.push_front(100);//头部插入
L1.push_front(200);
L1.push_front(300);
cout << "L1为" << endl;
printLsit(L1);
L1.pop_back();//pop_back尾部删除元素
cout << "新L1为" << endl;
printLsit(L1);
L1.pop_front();//头部删除元素
cout << "新L1为" << endl;
printLsit(L1);
//插入 第一个参数是迭代器
L1.insert(L1.begin(), 1000);
cout << "L1为" << endl;
printLsit(L1);
list<int>::iterator it = L1.begin();
L1.insert(it++, 666);
printLsit(L1);
//删除
it = L1.begin();
L1.erase(++it);
cout << "L1为" << endl;
printLsit(L1);
//移除
L1.push_back(10000);
L1.push_back(10000);
L1.push_back(10000);
L1.push_back(10000);
cout << "L1为" << endl;
printLsit(L1);
L1.remove(10000);
cout << "L1为" << endl;
printLsit(L1);
L1.clear();
cout << "L1为" << endl;
printLsit(L1);
}
int main()
{
test01();
system("pause");
return 0;
}
4、总结:
- 尾删 … push_back
- 尾插 … pop_back
- 头插 … push_front
- 尾删 … pop_front
- 插入 … insert
- 删除 … erase
- 移除 … remove
- 清空 … clear
5、运行结果
3.7.6、list数据存取
1、功能描述:
- 对list中的数据的存取操作
2、函数原型:
- front(); //返回容器中的第一个数据元素
- back(); //返回容器中的最后一个数据元素
3、代码
#include<iostream>
#include<list>
using namespace std;
void printDeque(const list<int>& lst)
{
for (list<int>::const_iterator it = lst.begin(); it !=lst.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_front(100);
L1.push_front(200);
//L1[0]不可以用[]访问list容器中的元素
//L1.at(0) 不可以用at方式来访问list容器中的元素
//原因是list本质链表,不是连续性的存储空间存储数据,迭代器也是不支持随机访问的
//通过front返回第一个元素
cout << L1.front() << endl;
//通过back返回最后一个元素
cout << L1.back() << endl;
//验证迭代器是不支持随机访问的
list<int>::iterator it = L1.begin();
//it + 1;//不支持随机访问
//只支持++和--只支持双向
it++;
it--;
}
int main()
{
test01();
system("pause");
return 0;
}
4、总结:
- list容器中不可以通过[]和at方式访问数据
- front返回容器得意个元素
- back返回容器最后一个元素
5、运行结果
3.7.7、list排序
1、功能描述:
- 利用算法实现对list容器进行排序
2、 算法:
- reverse(); //反转链表
- sort(); //链表排序
3、代码
#include<iostream>
#include<list>
#include<algorithm>//算法头文件
using namespace std;
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(20);
L.push_back(10);
L.push_back(50);
L.push_back(40);
L.push_back(30);
cout << "反转前的L" << endl;
printList(L);
L.reverse();
cout << "反转后的L" << endl;
printList(L);
}
bool myCompare(int v1,int v2)
{
//降序 就让第一个数 > 第二个数
return v1 > v2;
}
void test02()
{
list<int>L;
L.push_back(20);
L.push_back(10);
L.push_back(50);
L.push_back(40);
L.push_back(30);
cout << "排序前的L" << endl;
printList(L);
//所有不支持随机访问迭代器的容器,不可以使用标准算法
//不支持随机访问迭代器的容器,内部会提供对应一些算法
//排序 默认排序规则 从大到小 升序
L.sort();
cout << "排序后的的L" << endl;
printList(L);
L.sort(myCompare);
cout << "排序后的的L(降序)" << endl;
printList(L);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
4、//总结:
- 反转 … reverse
- 排序 … sort
5、运行结果
3.7.8 排序案列
1、案列描述:
- 将Person自定义数据类型进行排序,Person中属性有姓名,年龄,身高
2、 排序规则:
- 按照年龄进行升序,如果年龄相同按照升高进行降序
3、代码
#include<iostream>
#include<string>
#include<list>
using namespace std;
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_Age < p2.m_Age;
}
else
{ //年龄相同 按照 身高降序
return p1.m_Height > p2.m_Height;
}
}
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;
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;
}
4、总结:
- 对于自定义数据类型,必须要有指定的排序规则,否则不知道如何进行排序
- 高级排序只是在排序规则上进行一次逻辑制定,并不复杂
5、运行结果