1.list的根本概念:
功能:将数据举行链式存储
list(链表)是一种物理存储单位上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
链表是由一系列节点构成,节点的构成包含存储数据元素的数据域和存储下一个节点地点的指针域
STL中的链表是一个双向循环链表
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
list的优点:采用动态存储分配,不会造成内存浪费和溢出
链表执行插入和删除操作十分方便,修改指针即可,不须要像容器那样移动大量元素
list的缺点:链表更灵活,但空间(指针)和时间(遍历)额外泯灭较大
list的紧张性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector中是不建立的(vector插入元素大于开始容量时,会举行深拷贝,原有迭代器会失效)
留意:STL中list和vector容器是最常被使用的容器,各有优缺点
2.list的构造函数
- void Print_List(const list<int> L)
- {
- for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
- void test01()//list的操作
- {
- //1
- list<int> L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- L1.push_back(50);
- L1.push_front(5);
- Print_List(L1);
- //2
- list<int> L2(L1.begin(), L1.end());
- Print_List(L2);
- //3
- list<int> L3(5, 100);
- Print_List(L3);
- //4
- list<int> L4(L3);
- Print_List(L4);
- }
复制代码 3.list的赋值和交换操作
- void Print_List(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);
- L1.push_back(50);
- L1.push_front(5);
- Print_List(L1);
- //1
- list<int> L2;
- L2.assign(L1.begin(), L1.end());
- Print_List(L2);
- //2
- list<int> L3;
- L3.assign(5, 100);
- Print_List(L3);
- //3
- list<int> L4;
- L4 = L3;
- Print_List(L4);
- }
- void test02()//list的交换操作
- {
- list<int> L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- L1.push_back(50);
- L1.push_front(5);
- list<int> L2(5, 100);
- cout << "交换前:" << endl;
- Print_List(L1);
- Print_List(L2);
- L1.swap(L2);
- cout << "交换后:" << endl;
- Print_List(L1);
- Print_List(L2);
- }
复制代码 4.list的巨细操作
5.list的插入和删除操作
- void Print_List(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);
- Print_List(L1);
- //头插
- L1.push_front(100);
- L1.push_front(100);
- L1.push_front(100);
- Print_List(L1);
- //头删
- L1.pop_front();
- Print_List(L1);
- //尾删
- L1.pop_back();
- Print_List(L1);
- //insert插入
- list<int>::iterator it = L1.begin();
- L1.insert(++it, 50);//第一个参数是迭代器,先++在插入//注意只能用++不能用+n
- Print_List(L1);
- //删除
- it = L1.begin();
- L1.erase(++it);
- Print_List(L1);
- //移除,删除list中所有为m的元素
- L1.push_back(1000);
- L1.push_back(1000);
- L1.push_back(1000);
- L1.push_back(1000);
- Print_List(L1);
- L1.remove(1000);
- Print_List(L1);
- //清空
- L1.clear();
- Print_List(L1);
- }
复制代码 6.list的数据存取
不能用[]和at()访问,由于数据的内存地点不相连,不支持随机访问
it(迭代器)=it+1报错说明不支持随机访问
it++和it--报错一个说明说明单向链表,都不报错说明是双向链表
7.list的反转和排序
reserve是预留空间
reverse是反转
- void Print_List(const list<int> L)
- {
- for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
- bool myCompare(int a, int b)//比较函数
- {
- return a > b;//返回(第一个数大于第二个数相当于降序)反之升序
- }
- void test01()
- {
- list<int> L1;
- //尾插
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- Print_List(L1);
- L1.reverse();
- cout << "反转后" << endl;
- Print_List(L1);
- L1.push_front(50);
- L1.push_front(70);
- L1.push_front(60);
- Print_List(L1);
- cout << "排序后:" << endl;
- L1.sort();//不支持随机访问迭代器的容器里,不能用标准算法//但内部会提供一些算法
- //默认排序为升序
- Print_List(L1);
- L1.sort(myCompare);//降序
- cout << "降序后:" << endl;
- Print_List(L1);
- }
复制代码 8.高级排序的案例(对于自定义类型数据,必须自己制定排序规则举行高级排序)

- 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;
- };
- void Print_List(const list<Person> L)
- {
- for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++)
- {
- cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << " 身高:" << it->m_height << endl;
- }
- cout << endl;
- }
- bool myCompare(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;
- }
- //return (p1.m_age == p2.m_age ? p1.m_height > p2.m_height:p1.m_age < p2.m_age);//三目运算符
- }
- void test01()
- {
- Person p1("刘备", 30, 175);
- Person p2("张飞", 33, 175);
- Person p3("关羽", 30, 185);
- Person p4("赵云", 34, 176);
- Person p5("马超", 37, 178);
- list<Person> L;
- L.push_back(p1);
- L.push_back(p2);
- L.push_back(p3);
- L.push_back(p4);
- L.push_back(p5);
- Print_List(L);
- cout << "-------------------------------" << endl;
- cout << "排序后:" << endl;
- L.sort(myCompare);
- Print_List(L);
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |