一、基本概念
【list底层是由双向链表实现的,因此内存空间不是连续的。根据链表的实现原理,List查询效率较低,时间复杂度为O(n),但插入和删除效率较高。只需要在插入的地方更改指针的指向即可,不用移动数据。】
1、链表容器将数据进行链式存储。它是一种物理存储单元上非连续的存储结构,数据元素的逻辑是通过链表中的指针连接来实现的。
2、链表是由一系列的结点组成的。
3、结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
3、STL中的链表是一个双向循环链表:
- 由于链表的存储不是连续的空间,所以list的迭代器只支持前移和后移,属于双向迭代器。
4、list容器的优缺点
5、关于list容器和vector容器的区别:
二、常用接口
1、list的构造函数、赋值及交换
#include<iostream>
#include<string>
#include<list>
#include<algorithm>
using namespace std;
void PrintValue(const list<int>&s)
{
for (list<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << (*it) << " ";
}
}
void Test01()
{
list<int> L; // 默认构造函数
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
PrintValue(L);
// 区间构造函数
list<int>L2(L.begin(), L.end());
// 拷贝构造
list<int>L3(L);
// n个elm
list<int>L4(10, 99);
// 赋值
list<int>L5;
L5 = L; // =赋值
list<int>L6;
L6.assign(L.begin(), L.end());
list<int>L7;
L7.assign(10, 999); //赋值为10个999
//交换操作,将两个容器进行交换
L.swap(L7); // 将L容器和L7容器的值进行交换
}
int main()
{
Test01();
cin.get();
}
2、list的插入和删除
#include<iostream>
#include<string>
#include<list>
#include<algorithm>
using namespace std;
void PrintValue(const list<int>&s)
{
for (list<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << (*it) << " ";
}
}
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);
PrintValue(L);
L.pop_back(); // 尾删
L.pop_front(); //头删
//插入元素
list<int>::iterator it = L.begin();
L.insert(++it, 900); //开头指针向后偏移一个单位处(即第二个位置),插入900
// 删除元素
list<int>::iterator it2 = L.begin();
//将迭代器所指的位置处元素删除,此时it2所指的地址被销毁,可以将其他的迭代器赋给it2
L.erase(it2);
//移除元素: remove(elem) 删除容器中所有的elem元素
L.remove(100);
}
int main()
{
Test01();
cin.get();
}
3、list的数据存取,不能跳跃式的存取,只能通过 front()、back()来对数据进行访问。排序、反转。
#include<iostream>
#include<string>
#include<list>
#include<algorithm>
using namespace std;
void PrintValue(const list<int>&s)
{
for (list<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << (*it) << " ";
}
}
bool MyCompara(int v1,int v2)
{
// 降序就让第一个数大于第二个数
return v1 > v2;
}
void Test01()
{
list<int> L; // 默认构造函数
// 尾部插入
L.push_back(60);
L.push_back(20);
L.push_back(40);
L.push_back(10);
L.push_back(50);
cout << "第一个元素为:" << L.front() << endl;
cout << "最后一个元素为:" << L.back() << endl;
PrintValue(L);
cout << endl;
// 迭代器不支持随机访问
list<int>::iterator it = L.begin();
// 支持双向
it++;
it--;
// it=it+1; 随机访问是不支持的
L.reverse(); //反转所有数据
PrintValue(L);
cout << endl;
// 所有不支持随机访问迭代器的容器,不可以使用标准算法
// 不支持随机访问迭代器的容器,内部会提供对应的一些算法
//此时使用内部的成员函数,而不是全局函数。默认升序
L.sort();
PrintValue(L);
cout << endl;
// 降序
L.sort(MyCompara);
PrintValue(L);
}
int main()
{
Test01();
cin.get();
}
三、应用实例
1、要求:对自定义数据类型进行排序。按照年龄进行升序排列,如果年龄相同,则按身高进行降序。
2、注意:对于自定义数据类型需要指定排序规则。
#include<iostream>
#include<string>
#include<list>
#include<algorithm>
using namespace std;
class Person
{
public:
string m_Name;
int m_Age;
int m_Heigth;
Person(string name, int age, int heigth)
{
m_Name = name;
m_Age = age;
m_Heigth = heigth;
}
};
void PrintValue(const list<Person>&s)
{
for (list<Person>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名:" << (*it).m_Name << " " << "年龄:" << (*it).m_Age << " " << "身高:" << (*it).m_Heigth << endl;
}
}
// 指定排序规则
bool MyCompara(Person p1,Person p2)
{
// 如果年龄相同,按身高降序排列
if (p1.m_Age == p2.m_Age)
{
return p1.m_Heigth > p2.m_Heigth;
}
else
{
// 否则按年龄升序排列
return p1.m_Age < p2.m_Age;
}
}
void Test01()
{
list<Person> L;
Person p1("A", 18, 160);
Person p2("B", 29, 176);
Person p3("C", 12, 140);
Person p4("D", 60, 150);
Person p5("E", 18, 200);
Person p6("F", 27, 145);
Person p7("G", 18, 100);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
L.push_back(p7);
cout << "排序前" << endl;
PrintValue(L);
cout << "----------------------------" << endl;
//进行排序
L.sort(MyCompara);
cout << "排序后" << endl;
PrintValue(L);
}
int main()
{
Test01();
cin.get();
}