c++学习——list容器的使用学习

打印 上一主题 下一主题

主题 886|帖子 886|积分 2658

c++中的list容器其实就是我们前面在数据布局时学习过的  带头双向链表,在c++中,研发这将如许的数据布局举行封装。本日我们=就来举行深入的学习,来看看作为C++STL容器
之一,list暗含着什么样的秘密。
1.1list容器的先容

先容文档:cplusplus.com/reference/list/list/?kw=list

通过这幅图其实就可以简朴的解释list容器的根本布局, list本质上就是一个双向的循环带头链表
1.2list的使用

list容器和前面的string 和 vector容器非常的像,根本的功能本质上那个就是实现对数据布局中的造数据实现相应的增删查改。 
下买呢我们还是先来先容一些常见的list容器接口,来深入的学习list容器。

1.2.1 list的构造



构造函数(construct)接口说明
list (size_type n, const value_type& val = value_type())构造list中包含了n个值为val的元素
list()构造空的list
list(const list&x)构造拷贝函数
list(Inputiterator first, inputliterator last)用(first, last)区间中的元素构造list
1.2.1.1  list (size_type n, const value_type& val = value_type())

  1. #include<list>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.         list<int> s1(4,3);
  7. //迭代器的使用,后面会说
  8.         for (auto i : s1)
  9.         {
  10.                 cout << i << endl;
  11.         }
  12.         return 0;
  13. }
复制代码
 

 1.2.1.2 list(const list&x)


  1. #include<list>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.         list<int> s1(4, 3);
  7.        
  8.         for (auto i : s1)
  9.         {
  10.                 cout << i;
  11.         }
  12.         cout << endl;
  13.         s1.push_back(4);//在s1的尾部插入一个数据
  14.         for (auto i : s1)
  15.         {
  16.                 cout << i ;
  17.         }
  18.         cout << endl;
  19.         //演示list(const list& x)
  20.         list<int> s2(s1);
  21.         for (auto i : s1) cout << i;
  22.         cout << endl;
  23.         return 0;
  24. }
复制代码

1.2.1.3 list(Inputiterator first, inputliterator last)


通过迭代器来拷贝原来链表指定区域中的元素 
  1. list<int> s1 = { 1,2,3,4,5 };
  2. cout << "s1:";
  3. for (auto i : s1) cout << i;
  4. cout << endl;
  5. //演示 list(inputliretator first, inputliterator last)
  6. list<int>  s2(s1.begin(), s1.end());
  7. cout << "s2:";
  8. for (auto i : s2) cout << i;
  9. cout << endl;
复制代码



 1.2.2 list iterator的使用

函数的声明接口说明
begin + end返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin+rend返回第一个元素的 reverse_literator, 即end位置,返回最后一个元素下一个元素的 reverse_iterator, 即begin的位置

注意:
1. begin与end为正向迭代器, 对迭代器的执行 ++ 操作时,迭代器向后移动。
2. rbegin(end)与 rend(begin) 为反向迭代器, 对迭代器的++ 操作,迭代器向前移动。
 接下来是一些迭代器相干的代码展示:
        正向迭代器 
  1. #include<iostream>
  2. #include<list>
  3. using namespace std;
  4. // list迭代器的使用
  5. // 注意:遍历链表只能用迭代器和范围for
  6. void PrintList(const list<int>& l)
  7. {
  8.     // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
  9.     for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
  10.     {
  11.         cout << *it << " ";
  12.         // *it = 10; 编译不通过
  13.     }
  14.     cout << endl;
  15. }
复制代码


反向想迭代器  
  1. void TestList2()
  2. {
  3.     int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  4.     list<int> l(array, array + sizeof(array) / sizeof(array[0]));
  5.     // 使用正向迭代器正向list中的元素
  6.     // list<int>::iterator it = l.begin();   // C++98中语法
  7.     auto it = l.begin();                     // C++11之后推荐写法
  8.     while (it != l.end())
  9.     {
  10.         cout << *it << " ";
  11.         ++it;
  12.     }
  13.     cout << endl;
  14.     // 使用反向迭代器逆向打印list中的元素
  15.     // list<int>::reverse_iterator rit = l.rbegin();
  16.     auto rit = l.rbegin();
  17.     while (rit != l.rend())
  18.     {
  19.         cout << *rit << " ";
  20.         ++rit;
  21.     }
  22.     cout << endl;
  23. }
复制代码
 

 1.2.3 list capacity

函数申明接口说明
empty检测list是否为空, 是就返回true,否则返回false
size返回list中有效节点的个数
1.2.4 list element access

函数的声明接口说明
front返回list的第一个节点的值
back返回list的最后一个节点的值
  1. int main()
  2. {
  3.     list<int> l1;
  4.     //让程序随机生成 1-100之内的数字
  5.     srand((uint32_t)time(NULL));
  6.     for (int i = 0; i < 10; i++)
  7.     {
  8.         int x = rand() % 100;
  9.         l1.push_back(x);
  10.     }
  11.     //打印链表的元素
  12.     for (auto i : l1)
  13.     {
  14.         cout << i << " ";
  15.     }
  16.     cout << endl;
  17.     cout << "此时l1的大小是:" << l1.size() << endl;
  18.     cout << "链表的首元素:" << l1.front() << endl << "链表的尾元素:" << l1.back() << endl;
  19. }
复制代码

 1.2.5 list modifiers

函数申明接口说明
push_front 在list首元素前插入值为val1的元素
pop_front删除list元素中的第一个元素
push_back在list的尾部插入元素
pop_back删除list的最后一个元素
insert在list position位置插入元素
swap交换两个list中的元素
erase删除指定位置的值
clear清除list中的有效元素
关于上上面的接口,这里博主就提供一些测试用的函数,各人可以拿去测试一下:
 
  1.     // list插入和删除
  2. // push_back/pop_back/push_front/pop_front
  3.     void TestList3()
  4.     {
  5.         int array[] = { 1, 2, 3 };
  6.         list<int> L(array, array + sizeof(array) / sizeof(array[0]));
  7.         // 在list的尾部插入4,头部插入0
  8.         L.push_back(4);
  9.         L.push_front(0);
  10.         PrintList(L);
  11.         // 删除list尾部节点和头部节点
  12.         L.pop_back();
  13.         L.pop_front();
  14.         PrintList(L);
  15.     }
  16.     // insert /erase
  17.     void TestList4()
  18.     {
  19.         int array1[] = { 1, 2, 3 };
  20.         list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));
  21.         // 获取链表中第二个节点
  22.         auto pos = ++L.begin();
  23.         cout << *pos << endl;
  24.         // 在pos前插入值为4的元素
  25.         L.insert(pos, 4);
  26.         PrintList(L);
  27.         // 在pos前插入5个值为5的元素
  28.         L.insert(pos, 5, 5);
  29.         PrintList(L);
  30.         // 在pos前插入[v.begin(), v.end)区间中的元素
  31.         vector<int> v{ 7, 8, 9 };
  32.         L.insert(pos, v.begin(), v.end());
  33.         PrintList(L);
  34.         // 删除pos位置上的元素
  35.         L.erase(pos);
  36.         PrintList(L);
  37.         // 删除list中[begin, end)区间中的元素,即删除list中的所有元素
  38.         L.erase(L.begin(), L.end());
  39.         PrintList(L);
  40.     }
  41.     // resize/swap/clear
  42.     void TestList5()
  43.     {
  44.         // 用数组来构造list
  45.         int array1[] = { 1, 2, 3 };
  46.         list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
  47.         PrintList(l1);
  48.         // 交换l1和l2中的元素
  49.         list<int> l2;
  50.         l1.swap(l2);
  51.         PrintList(l1);
  52.         PrintList(l2);
  53.         // 将l2中的元素清空
  54.         l2.clear();
  55.         cout << l2.size() << endl;
  56.     }
复制代码
好,本日的学习就到这里,我们下期再见,拜!!

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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

钜形不锈钢水箱

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

标签云

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