标准STL中list的各个接口的使用

Myth丶恋晨 2022-06-17 08:14 220阅读 0赞

list是标准STL的序列式容器,它里面的元素是有序的线性序列
STL中的list就是一个带头结点的循环双向链表,可以高效的删除和插入元素。
但是list是不支持随机访问的,因为它的存储空间是不连续的。
而且标准的STL的list我们是通过迭代器来遍历容器里的元素的,迭代器是一种检查容器内元素并遍历元素的数据类型。
标准STL中list的成员函数
这里写图片描述

constructor:list的成员函数
1、explicit list ( const Allocator& = Allocator() );
默认的构造函数这个链表的是空的。
2、explicit list ( size_type n, const T& value = T(), const Allocator& = Allocator() );
带参数的构造函数,构造有n个值为value结点
3、template < class InputIterator >
list ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
构造函数的参数类型为迭代器的类型
4、list ( const list

  1. #include<iostream>
  2. #include<list>
  3. using namespace std;
  4. void FunTest()
  5. {
  6. list<int>first;
  7. //使用默认的构造函数创造first这个对象,但是这个对象里面的内容是空的
  8. list<int>second(4, 100);
  9. //使用带参数的构造函数去创建4个值为100的结点的对象
  10. list<int>third(second.begin(), second.end());
  11. //使用带迭代器参数的构造函数将一段区间的内容复制过来
  12. list<int>forth(third);
  13. //拷贝构造函数
  14. list<int>::iterator it1;//使用迭代器去遍历first对象里的元素
  15. list<int>::iterator it2;//使用迭代器去遍历second对象里的元素
  16. list<int>::iterator it3;//使用迭代器去遍历third对象里的元素
  17. list<int>::iterator it4;//使用迭代器去遍历forth对象里的元素
  18. it1 = first.begin();
  19. while (it1 != first.end())
  20. {
  21. cout << *it1 << " ";
  22. ++it1;
  23. }
  24. cout << endl;
  25. it2 = second.begin();
  26. while (it2 != second.end())
  27. {
  28. cout << *it2 << " ";
  29. ++it2;
  30. }
  31. cout << endl;
  32. it3 = third.begin();
  33. while (it3 != third.end())
  34. {
  35. cout << *it3 << " ";
  36. ++it3;
  37. }
  38. cout << endl;
  39. it4 = forth.begin();
  40. while (it4 != forth.end())
  41. {
  42. cout << *it4 << " ";
  43. ++it4;
  44. }
  45. cout << endl;
  46. }

这里写图片描述

2、destructor关于析构函数的使用:
~list ( );
它是用来回收我们之前分配的内存,当然STL中的内存分配是由它自己的空间配置器来分配内存的

3、operator=对运算符’=’的重载

  1. void FunTest2()
  2. {
  3. list<int> first(5, 10);
  4. list<int> second(3, 1);
  5. list<int>::iterator it2;
  6. it2 = second.begin();
  7. while (it2 != second.end())
  8. {
  9. cout << *it2 << " ";
  10. ++it2;
  11. }
  12. cout << endl;
  13. list<int>::iterator it1;
  14. second = first;
  15. it1= second.begin();
  16. while (it1 != second.end())
  17. {
  18. cout << *it1 << " ";
  19. ++it1;
  20. }
  21. cout << endl;
  22. }

4、迭代器的使用
1>begin()
iterator begin ();
const_iterator begin () const;
2>end();
iterator end ();
const_iterator end () const;

  1. void FunTest3()
  2. {
  3. list<int> l(10,100);
  4. list<int>::iterator it1=l.begin();//l.begin返回的是非const类型的迭代器是可以修改的
  5. while (it1 != l.end())
  6. {
  7. *it1=10;//迭代器里的解引用这个操作符是重载过的表示迭代器里结点里的data的值
  8. cout << *it1 << " ";
  9. ++it1;
  10. }
  11. cout << endl;
  12. }
  13. void FunTest4()
  14. {
  15. list<int>l(10, 2);
  16. list<int>::const_iterator it = l.begin();
  17. while (it != l.end())
  18. {
  19. //*it = 10;//是不能赋值的因为const修饰的迭代器表示这里面的内容是不可以修改的
  20. cout << *it << " ";
  21. ++it;
  22. }//以上的迭代器都是正向遍历容器里的元素的
  23. }

3>rbegin():
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
3>rend():
reverse_iterator rend();
const_reverse_iterator rend() const;

  1. void FunTest5()
  2. {
  3. list<int>l;
  4. l.push_back(1);
  5. l.push_back(2);
  6. l.push_back(3);
  7. l.push_back(4);
  8. list<int>::reverse_iterator it = l.rbegin();
  9. while (it != l.rend())
  10. {
  11. cout << *it << " ";
  12. ++it;
  13. }
  14. }//打印出来的结果是4,3,2,1,因为这个迭代器是反向遍历的,它的头相当于正向迭代器的尾

5、Capacity:关于容量的一些方法
empty();
bool empty ( ) const;
size()
size_type size() const();

max_size():
size_type max_size () const;

resize()://具有增容和裁剪的作用
void resize ( size_type sz, T c = T() );

  1. void FunTest6()
  2. {
  3. list<int>l(10);
  4. cout << l.empty()<<endl;//判断链表空不空
  5. cout << l.size()<<endl;//结点的个数
  6. cout << l.max_size()<<endl;
  7. l.resize(5);//相当于裁减让链表只剩下5个结点
  8. cout << l.size() << endl;
  9. l.resize(10, 8);//将链表变成10个值为8的结点
  10. cout << l.size() << endl;
  11. }

front():
reference front ( );
const_reference front ( ) const;
back();
reference back ( );
const_reference back ( ) const;

  1. void FunTest7()
  2. {
  3. list<int>l(10, 2);
  4. l.push_back(8);
  5. cout << l.front()<<endl;
  6. cout << l.back();
  7. }//front和back是以引用值的方式返回的

5、Modifiers:关于修改链表的 内容
1>assign():
template
void assign ( InputIterator first, InputIterator last );//参数直接给成迭代器类型的,赋值的是一段区间
void assign ( size_type n, const T& u );//直接赋值给链表值为u的结点

  1. void FunTest8()
  2. {
  3. list<int>first;
  4. list<int>second(10,20);
  5. first.assign(7, 10);
  6. cout << first.size()<<endl;
  7. list<int>::iterator it1;
  8. it1 = first.begin();
  9. while (it1 != first.end())
  10. {
  11. cout << *it1 << " ";
  12. ++it1;
  13. }
  14. cout << endl;
  15. first.assign(second.begin(), second.end());
  16. list<int>::iterator it2;
  17. it2 = first.begin();
  18. while (it2 != first.end())
  19. {
  20. cout << *it2 << " ";
  21. ++it2;
  22. }
  23. cout << endl;
  24. cout << first.size();
  25. }//list中assign的使用

2>push_front():
void push_front ( const T& x );
3>push_back():
void push_back ( const T& x );
4>pop_front():
void pop_front ( )
5>pop_back():
void pop_back ( );

  1. void FunTest9()
  2. {
  3. list<int> l;
  4. l.push_back(1);
  5. l.push_back(2);
  6. l.push_back(3);
  7. l.push_back(4);
  8. l.push_back(5);
  9. l.pop_back();
  10. l.pop_back();
  11. l.push_front(7);
  12. l.push_front(8);
  13. l.push_front(9);
  14. l.pop_front();
  15. l.pop_front();
  16. list<int>::iterator it2;
  17. it2 = l.begin();
  18. while (it2 != l.end())
  19. {
  20. cout << *it2 << " ";
  21. ++it2;
  22. }
  23. cout << endl;
  24. }//list中push_back的用法,pop_back,push_front,pop_front的用法

6>insert():
iterator insert ( iterator position, const T& x );
void insert ( iterator position, size_type n, const T& x );
template
void insert ( iterator position, InputIterator first, InputIterator last );

  1. void FunTest10()
  2. {
  3. list<int>first;
  4. list<int>second;
  5. list<int>third;
  6. //iterator insert ( iterator position, const T& x );
  7. first.insert(first.begin(), 8);
  8. //void insert(iterator position, size_type n, const T& x);
  9. second.insert(second.begin(), 4, 9);
  10. //void insert ( iterator position, InputIterator first, InputIterator last );
  11. third.insert(third.begin(), second.begin(), second.end());
  12. list<int>::iterator it2;
  13. it2 = first.begin();
  14. while (it2 != first.end())
  15. {
  16. cout << *it2 << " ";
  17. ++it2;
  18. }
  19. cout << endl;
  20. list<int>::iterator it3;
  21. it3 = second.begin();
  22. while (it3 != second.end())
  23. {
  24. cout << *it3 << " ";
  25. ++it3;
  26. }
  27. cout << endl;
  28. list<int>::iterator it4;
  29. it4= second.begin();
  30. while (it4 != second.end())
  31. {
  32. cout << *it4 << " ";
  33. ++it4;
  34. }
  35. cout << endl;
  36. }//list中insert的用法

7>erase():
iterator erase ( iterator position );
iterator erase ( iterator first, iterator last );

  1. void FunTest11()
  2. {
  3. list<int>l(10, 2);
  4. //iterator erase(iterator position);
  5. l.erase(l.begin());
  6. cout << l.size()<<endl;
  7. //iterator erase(iterator first, iterator last);
  8. l.erase(l.begin(), l.end());
  9. cout << l.size()<<endl;
  10. }//erase的用法

8>swap():
void swap ( list

  1. void FunTest12()
  2. {
  3. list<int>l1(10, 2);
  4. list<int>l2(10, 3);
  5. l2.swap(l1);
  6. list<int>::iterator it4;
  7. it4 = l1.begin();
  8. cout << "l1:";
  9. while (it4 !=l1.end())
  10. {
  11. cout << *it4 << " ";
  12. ++it4;
  13. }
  14. cout << endl;
  15. cout << "12:";
  16. list<int>::iterator it5=l2.begin();
  17. while (it5 != l2.end())
  18. {
  19. cout << *it5 << " ";
  20. ++it5;
  21. }
  22. }//list中swap的用法将两个链表的内容交换

9>clear():
void clear ( );list中clear的用法,清除所有元素

  1. void FunTest13()
  2. {
  3. list<int>l(10,5);
  4. cout<<l.size()<<endl;
  5. l.clear();
  6. cout << l.size();
  7. }//list中clear的用法,清除所有元素

6、Operations:
1>splice():
void splice ( iterator position, list

  1. void FunTest14()
  2. {
  3. //void splice(iterator position, list<T, Allocator>& x);
  4. list<int>l1(10, 12);
  5. list<int>l2(10, 13);
  6. l1.splice(l1.begin(), l2);//把l1的元素从头全部搬移到l2中
  7. list<int>::iterator it5 = l2.begin();
  8. while (it5 != l2.end())
  9. {
  10. cout << *it5 << " ";
  11. ++it5;
  12. }
  13. cout << endl;
  14. list<int>::iterator it6 = l1.begin();
  15. while (it6 != l1.end())
  16. {
  17. cout << *it6 << " ";
  18. ++it6;
  19. }
  20. cout << endl;
  21. //void splice(iterator position, list<T, Allocator>& x, iterator i);
  22. list<int>l3(10, 1);
  23. list<int>l4(10, 2);
  24. list<int>::iterator it7 = l4.begin();
  25. ++it7;
  26. l3.splice(l3.begin(), l4,it7);
  27. list<int>::iterator it8 = l4.begin();
  28. while (it8 != l4.end())
  29. {
  30. cout << *it8 << " ";
  31. ++it8;
  32. }
  33. cout << endl;
  34. list<int>::iterator it9 = l3.begin();
  35. while (it9 != l3.end())
  36. {
  37. cout << *it9 << " ";
  38. ++it9;
  39. }
  40. cout << endl;
  41. //void splice(iterator position, list<T, Allocator>& x, iterator first, iterator last);
  42. list<int>l5(10, 100);
  43. list<int>l6(10, 200);
  44. l5.splice(l5.begin(), l6, ++l6.begin(), l6.end());//把l6从++l6.begin()到l6.end()个元素全部搬移到l5中
  45. list<int>::iterator it10 = l5.begin();
  46. while (it10 != l5.end())
  47. {
  48. cout << *it10<<" ";
  49. it10++;
  50. }
  51. cout << endl;
  52. }//list中splice的用法

2>remove():
void remove ( const T& value );

  1. void FunTest15()
  2. {
  3. list<int>l1(10, 8);
  4. l1.remove(8);//移除l1中所有值为8的结点
  5. cout << l1.size();
  6. }//list中remove的用法

3>remove_if():
template
void remove_if ( Predicate pred );//对这个用法还不太了解

4>unique():
void unique ( );//删除重复的元素只保留一份
template
void unique ( BinaryPredicate binary_pred );

  1. void FunTest16()
  2. {
  3. /*void unique(); template <class BinaryPredicate> void unique(BinaryPredicate binary_pred);*/
  4. list<int>l1(10, 100);
  5. l1.unique();//只保留一份,移除重复元素
  6. cout << l1.size();
  7. }

5>merge():
void merge ( list

  1. void FunTest17()
  2. {
  3. //void merge(list<T, Allocator>& x);
  4. list<int>l1(10, 1);
  5. list<int>l2(10, 2);
  6. l1.merge(l2);//把l2中所有的元素都合并到l1中
  7. list<int>::iterator it10 = l1.begin();
  8. while (it10 != l1.end())
  9. {
  10. cout << *it10 << " ";
  11. it10++;
  12. }
  13. cout << endl;
  14. /*template <class Compare> void merge(list<T, Allocator>& x, Compare comp);*/
  15. }

6>sort()://对链表的的无序元素进行排序
void sort ( );
template
void sort ( Compare comp );//涉及到仿函数的使用决定是升序排列还是降序排列

  1. void FunTest18()
  2. {
  3. list<int>l;
  4. l.push_back(3);
  5. l.push_back(1);
  6. l.push_back(4);
  7. l.sort();
  8. list<int>::iterator it1 = l.begin();
  9. while (it1 != l.end())
  10. {
  11. cout << *it1 << " ";
  12. ++it1;
  13. }
  14. }

发表评论

表情:
评论列表 (有 0 条评论,220人围观)

还没有评论,来说两句吧...

相关阅读