必看!用示例代码学C++ STL-list,快速掌握基础知识,高效提升编程能力

56 阅读5分钟

image.png

C++ STL-list


目录

C++ STL-list

●1.list的构造

●2.list iterator的使用

●3.list的容量

●4.list元素的访问

●5.list的修改

●6.list迭代器的失效

●7.list的模拟实现


1.list的构造

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

示例:

template<class T>
void print_list(const list<T> &l)
{
       auto p = l.begin();
       while (p != l.end())
       {
               cout << *p;
               p++;
       }
       cout << endl;
}
void test1()
{
       list<int> l;
       print_list(l);//
       list<int> l1(10, 1);
       print_list(l1);//1111111111
       list<int> l2(l1);
       print_list(l2);//1111111111
       list<int>::iterator startp = l1.begin();
       list<int>::iterator endp = l1.end();
       list<int> l3(startp, endp);
       print_list(l3);//1111111111
}

2.list iterator的使用

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

示例:

void test2()
{
       list<int> l;
       l.push_back(1);
       l.push_back(2);
       l.push_back(3);
       l.push_back(4);
       l.push_back(5);
       list<int>::iterator p;
       p = l.begin();
       cout << *p << endl;   //1
       p = l.end();
       p--;
       cout << *p << endl;   //5
       list<int>::reverse_iterator q;
       q = l.rend();
       q--;
       cout << *q << endl;   //1
       q = l.rbegin();
       cout << *q << endl;   //5
}

3.list的容量

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

示例:

void test3()
{
       list<int> l;
       l.push_back(1);
       l.push_back(2);
       l.push_back(3);
       l.push_back(4);
       l.push_back(5);
       cout << l.empty() << endl;//0
       cout << l.size() << endl;//5
}

4.list元素的访问

函数声明接口说明
front返回list的第一个节点中值的引用
back返回list的最后一个节点中值的引用

示例:

void test4()
{
       list<int> l;
       l.push_back(1);
       l.push_back(2);
       l.push_back(3);
       l.push_back(4);
       l.push_back(5);
       cout << l.front() << endl;    //1
       cout << l.back() << endl;     //5
}

5.list的修改

函数声明接口说明
push_front在list首元素前插入值为val的元素
pop_front删除list中第一个元素
push_back在list尾部插入值为val的元素
pop_back删除list中最后一个元素
insert在list position 位置中插入值为val的元素
erase删除list position位置的元素
swap交换两个list中的元素
clear清空list中的有效元素

示例:

void test5()
{
       list<int> l;
       l.push_front(1);
       l.push_front(2);
       l.push_front(3);
       print_list(l); //321
       l.pop_front();
       l.pop_front();
       l.pop_front();
       print_list(l); //
 
       l.push_back(1);
       l.push_back(2);
       l.push_back(3);
       print_list(l); //123
       l.pop_back();
       l.pop_back();
       l.pop_back();
       print_list(l); //
 
       l.push_back(1);
       l.push_back(2);
       l.push_back(3);
       list<int>::iterator p = l.begin();
       p++;
       l.insert(p,0);
       print_list(l);//1023
       p++;
       l.insert(p, 0);
       print_list(l);//10203
 
       list<int>::iterator q = l.begin();
       q++;
       l.erase(q);
       print_list(l);//1203
       q = l.end();
       q--;
       q--;
       l.erase(q);
       print_list(l);//123
 
       list<int> l1;
       l.swap(l1);
       print_list(l);//
       print_list(l1);//123
       l1.clear();
       print_list(l1);//
}

6.list迭代器的失效

迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

示例:

void TestListIterator1()
{
       int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
       list<int> l(array, array + sizeof(array) / sizeof(array[0]));
       auto it = l.begin();
       while (it != l.end())
       {
               // erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给
               其赋值
                      l.erase(it);
               ++it;
       }
}
 
// 改正
void TestListIterator()
{
       int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
       list<int> l(array, array + sizeof(array) / sizeof(array[0]));
       auto it = l.begin();
       while (it != l.end())
       {
               l.erase(it++); // it = l.erase(it);
       }
}

7.list的模拟实现

示例:list.hpp(源头不分离)

#include"reverse_iterator.h"
namespace myList
{
       template<class T>
       struct list_node {
               T _data;
               list_node<T>* _prev;
               list_node<T>* _next;
               
               list_node(const T& x=T())
                      :_data(x)
                      ,_prev(nullptr)
                      ,_next(nullptr)
               {}
       };
 
       /*根据底层代码,进行代码优化*/
       template<class T,class Ref,class Ptr>
       struct __list_iterator {
               typedef list_node<T> node;
               typedef __list_iterator<T,Ref,Ptr> self;
               typedef Ref reference;
               typedef Ptr pointer;
               
               node* _node;
               __list_iterator(node *__node)
                      :_node(__node)
               {}
               reference operator*()
               {
                      return _node->_data;
               }
               /*比较特殊*/
               pointer operator->()
               {
                      return &_node->_data;
               }
               /*后置*/
               self& operator++()
               {
                      _node = _node->_next;
                      return *this;
               }
               /*前置*/
               self operator++(int)
               {
                      self tmp(*this);
                      _node = _node->_next;
                      return tmp;
               }
               self operator+=(int num)
               {
                      self tmp(*this);
                      while (num--)
                      {
                              _node = _node->_next;
                      }
                      return tmp;
               }
               /*后置*/
               self& operator--()
               {
                      _node = _node->_prev;
                      return *this;
               }
               /*前置*/
               self operator--(int)
               {
                      self tmp(*this);
                      _node = _node->_prev;
                      return tmp;
               }
               self operator-=(int num)
               {
                      self tmp(*this);
                      while (num--)
                      {
                              _node = _node->_prev;
                      }
                      return tmp;
               }
               bool operator!=(const self& l)
               {
                      return _node != l._node;
               }
               bool operator==(const self& l)
               {
                      return _node == l._node;
               }
       };
 
       template<class T>
       class list
       {
       public:
               typedef list_node<T> node;
               
               typedef __list_iterator<T, T&, T*> iterator;
               typedef __list_iterator<T, const T&, const T*> const_iterator;
       
               typedef reverseiterator<iterator,T&,T*> reverse_iterator;
               typedef reverseiterator<const_iterator, const T&, const  T*>const_reverse_iterator;
       public:
               void empty_init()
               {
                      _head = new node;
                      _head->_prev = _head;
                      _head->_next = _head;
                      _size=0;
               }
               /*构造函数初始化头结点*/
               list()
               {
                      empty_init();
               }
               list(list<T>& l)
               {
                      empty_init();
                      for (list<T>::iterator p = l.begin(); p != l.end(); p++)
                      {
                              push_back(*p);
                      }
               }
               /*l_new(l_old)*/
               list(const list<T>& l)
               {
                      empty_init();
                      for (auto e : l)
                      {
                              push_back(e);
                      }
               }
               void swap(list<T> l)
               {
                      std::swap(_head, l._head);
                      std::swap(_size, l._size);
               }
               list<T>& operator=(list<T>& l)
               {
                      swap(l);
                      return *this;
               }
               /*析构函数销毁头结点*/
               ~list()
               {
                      clear();
                      delete _head;
                      _head = nullptr;
               }
       public:
               reverse_iterator rbegin()
               {
                      return reverse_iterator(--end());
               }
               reverse_iterator rend()
               {
                      return reverse_iterator(end());
               }
               const_reverse_iterator rbegin() const
               {
                      return reverse_iterator(--end());
               }
               const_reverse_iterator rend() const
               {
                      return reverse_iterator(end());
               }
       public:
               /*指向空间内容可修改,可读*/
               iterator begin()
               {
                      return _head->_next;
               }
               iterator end()
               {
                      return _head;
               }
               /*指向空间内容不可修改,可读*/
               const_iterator begin() const
               {
                      return _head->_next;
               }
               const_iterator end() const
               {
                      return _head;
               }
       public:
               size_t size()
               {
                      return _size;
               }
               void clear()
               {
                      iterator p = begin();
                      while (p!=end())
                      {
                              p=erase(p);
                      }
               }
               void push_back(const T& x)
               {
                      node* newnode = new node(x);
                      node* _tail = _head->_prev;
                      
                      _tail->_next = newnode;
                      newnode->_prev = _tail;
                      newnode->_next = _head;
                      _head->_prev = newnode;
                      _size++;
               }
               /*复用*/
               //void push_back(const T& x=T())
               //{
               //     insert(end()--);
               //}
               void push_front(const T& x)
               {
                      insert(begin());
               }
               void pop_back()
               {
                      erase(--end());
               }
               void pop_front()
               {
                      erase(begin());
               }
               void insert(iterator pos,const T& x)
               {
                      node* newnode = new node(x);
                      node* cur = pos._node;
                      node* prev = cur->_prev;
                      
                      newnode->_prev = prev;
                      prev->_next = newnode;
                      newnode->_next = cur;
                      cur->_prev = newnode;
                      _size++;
               }
               iterator erase(iterator pos)
               {
                      node* cur = pos._node;
                      node* prev_node = cur->_prev;
                      node* next_node = cur->_next;
                      delete cur;
                      
                      prev_node->_next = next_node;
                      next_node->_prev = prev_node;
                      _size--;
                      /*防止指针失效*/
                      return iterator(next_node);
               }
               
       private:
               node* _head;
               size_t _size;
       };
}

示例代码:reverse_iterator.hpp(源头不分离)

#pragma once
template<class Iterator,class Ref,class Ptr>
class reverseiterator {
public:
       typedef reverseiterator<Iterator,Ref,Ptr> self;
       reverseiterator(Iterator it)
               :_it(it)
       {}
       self& operator++()
       {
               --_it;
               return *this;
       }
       self& operator++(int)
       {
               _it--;
               return *this;
       }
       self& operator--()
       {
               ++_it;
               return *this;
       }
       self& operator--(int)
       {
               _it++;
               return *this;
       }
       Ref operator*()
       {
               return *_it;
       }
       Ptr operator->()
       {
               return _it.operator->();
       }
       bool operator!=(const self& l)
       {
               return _it != l._it;
       }
private:
       Iterator _it;
};

<您的三连和关注是我最大的动力>

🚀 文章作者:张同学的IT技术日记

分类专栏:C++系列