list(链表)容器的规则及list的高级排序案例

打印 上一主题 下一主题

主题 548|帖子 548|积分 1644

1.list的根本概念:
功能:将数据举行链式存储
list(链表)是一种物理存储单位上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
链表是由一系列节点构成,节点的构成包含存储数据元素的数据域和存储下一个节点地点的指针域
STL中的链表是一个双向循环链表


 由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
list的优点:采用动态存储分配,不会造成内存浪费和溢出
链表执行插入和删除操作十分方便,修改指针即可,不须要像容器那样移动大量元素
list的缺点:链表更灵活,但空间(指针)和时间(遍历)额外泯灭较大
list的紧张性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector中是不建立的(vector插入元素大于开始容量时,会举行深拷贝,原有迭代器会失效)
留意:STL中list和vector容器是最常被使用的容器,各有优缺点
 2.list的构造函数

  1. void Print_List(const list<int> L)
  2. {
  3.         for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  4.         {
  5.                 cout << *it << " ";
  6.         }
  7.         cout << endl;
  8. }
  9. void test01()//list的操作
  10. {
  11.         //1
  12.         list<int> L1;
  13.         L1.push_back(10);
  14.         L1.push_back(20);
  15.         L1.push_back(30);
  16.         L1.push_back(40);
  17.         L1.push_back(50);
  18.         L1.push_front(5);
  19.         Print_List(L1);
  20.         //2
  21.         list<int> L2(L1.begin(), L1.end());
  22.         Print_List(L2);
  23.         //3
  24.         list<int> L3(5, 100);
  25.         Print_List(L3);
  26.         //4
  27.         list<int> L4(L3);
  28.         Print_List(L4);
  29. }
复制代码
 3.list的赋值和交换操作

 
  1. void Print_List(const list<int> L)
  2. {
  3.         for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  4.         {
  5.                 cout << *it << " ";
  6.         }
  7.         cout << endl;
  8. }
  9. void test01()//list的赋值操作
  10. {
  11.         list<int> L1;
  12.         L1.push_back(10);
  13.         L1.push_back(20);
  14.         L1.push_back(30);
  15.         L1.push_back(40);
  16.         L1.push_back(50);
  17.         L1.push_front(5);
  18.         Print_List(L1);
  19.         //1
  20.         list<int> L2;
  21.         L2.assign(L1.begin(), L1.end());
  22.         Print_List(L2);
  23.         //2
  24.         list<int> L3;
  25.         L3.assign(5, 100);
  26.         Print_List(L3);
  27.         //3
  28.         list<int> L4;
  29.         L4 = L3;
  30.         Print_List(L4);
  31. }
  32. void test02()//list的交换操作
  33. {
  34.         list<int> L1;
  35.         L1.push_back(10);
  36.         L1.push_back(20);
  37.         L1.push_back(30);
  38.         L1.push_back(40);
  39.         L1.push_back(50);
  40.         L1.push_front(5);
  41.         list<int> L2(5, 100);
  42.         cout << "交换前:" << endl;
  43.         Print_List(L1);
  44.         Print_List(L2);
  45.         L1.swap(L2);
  46.         cout << "交换后:" << endl;
  47.         Print_List(L1);
  48.         Print_List(L2);
  49. }
复制代码
4.list的巨细操作

 5.list的插入和删除操作

 
  1. void Print_List(const list<int> L)
  2. {
  3.         for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  4.         {
  5.                 cout << *it << " ";
  6.         }
  7.         cout << endl;
  8. }
  9. void test01()//list的插入和删除操作
  10. {
  11.         list<int> L1;
  12.         //尾插
  13.         L1.push_back(10);
  14.         L1.push_back(20);
  15.         L1.push_back(30);
  16.         Print_List(L1);
  17.         //头插
  18.         L1.push_front(100);
  19.         L1.push_front(100);
  20.         L1.push_front(100);
  21.         Print_List(L1);
  22.         //头删
  23.         L1.pop_front();
  24.         Print_List(L1);
  25.         //尾删
  26.         L1.pop_back();
  27.         Print_List(L1);
  28.         //insert插入
  29.         list<int>::iterator it = L1.begin();
  30.         L1.insert(++it, 50);//第一个参数是迭代器,先++在插入//注意只能用++不能用+n
  31.         Print_List(L1);
  32.         //删除
  33.         it = L1.begin();
  34.         L1.erase(++it);
  35.         Print_List(L1);
  36.         //移除,删除list中所有为m的元素
  37.         L1.push_back(1000);
  38.         L1.push_back(1000);
  39.         L1.push_back(1000);
  40.         L1.push_back(1000);
  41.         Print_List(L1);
  42.         L1.remove(1000);
  43.         Print_List(L1);
  44.         //清空
  45.         L1.clear();
  46.         Print_List(L1);
  47. }
复制代码
6.list的数据存取
不能用[]和at()访问,由于数据的内存地点不相连,不支持随机访问
it(迭代器)=it+1报错说明不支持随机访问
it++和it--报错一个说明说明单向链表,都不报错说明是双向链表

 7.list的反转和排序
reserve是预留空间
reverse是反转

 
  1. void Print_List(const list<int> L)
  2. {
  3.         for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
  4.         {
  5.                 cout << *it << " ";
  6.         }
  7.         cout << endl;
  8. }
  9. bool myCompare(int a, int b)//比较函数
  10. {
  11.         return a > b;//返回(第一个数大于第二个数相当于降序)反之升序
  12. }
  13. void test01()
  14. {
  15.         list<int> L1;
  16.         //尾插
  17.         L1.push_back(10);
  18.         L1.push_back(20);
  19.         L1.push_back(30);
  20.         Print_List(L1);
  21.         L1.reverse();
  22.         cout << "反转后" << endl;
  23.         Print_List(L1);
  24.         L1.push_front(50);
  25.         L1.push_front(70);
  26.         L1.push_front(60);
  27.         Print_List(L1);
  28.         cout << "排序后:" << endl;
  29.         L1.sort();//不支持随机访问迭代器的容器里,不能用标准算法//但内部会提供一些算法
  30.         //默认排序为升序
  31.         Print_List(L1);
  32.         L1.sort(myCompare);//降序
  33.         cout << "降序后:" << endl;
  34.         Print_List(L1);
  35. }
复制代码
8.高级排序的案例(对于自定义类型数据,必须自己制定排序规则举行高级排序)

 
  1. class Person
  2. {
  3. public:
  4.         Person(string name,int age,int height)
  5.         {
  6.                 this->m_name = name;
  7.                 this->m_age = age;
  8.                 this->m_height = height;
  9.         }
  10.         string m_name;
  11.         int m_age;
  12.         int m_height;
  13. };
  14. void Print_List(const list<Person> L)
  15. {
  16.         for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++)
  17.         {
  18.                 cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << " 身高:" << it->m_height << endl;
  19.         }
  20.         cout << endl;
  21. }
  22. bool myCompare(Person p1,Person p2)//比较函数
  23. {
  24.         if (p1.m_age == p2.m_age)//排序规则:优先按年龄升序,相同则身高降序
  25.         {
  26.                 return p1.m_height > p2.m_height;
  27.         }
  28.         else
  29.         {
  30.                 return p1.m_age < p2.m_age;
  31.         }
  32.         //return (p1.m_age == p2.m_age ? p1.m_height > p2.m_height:p1.m_age < p2.m_age);//三目运算符
  33. }
  34. void test01()
  35. {
  36.         Person p1("刘备", 30, 175);
  37.         Person p2("张飞", 33, 175);
  38.         Person p3("关羽", 30, 185);
  39.         Person p4("赵云", 34, 176);
  40.         Person p5("马超", 37, 178);
  41.         list<Person> L;
  42.         L.push_back(p1);
  43.         L.push_back(p2);
  44.         L.push_back(p3);
  45.         L.push_back(p4);
  46.         L.push_back(p5);
  47.         Print_List(L);
  48.         cout << "-------------------------------" << endl;
  49.         cout << "排序后:" << endl;
  50.         L.sort(myCompare);
  51.         Print_List(L);
  52. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

羊蹓狼

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表