目录
1. list的介绍及使用
1.1 list的介绍
1.2 list的使用
1.2.1 list的构造
1.2.2 list iterator的使用
1.2.3 list capacity
1.2.4 list element access
1.2.5 list modififiers
1.2.6 list的迭代器失效
2. list的深度剖析及模拟实现
2.1 模拟实现list
2.2 对模拟的bite::list进行测试
3. list与vector的对比
1. list的介绍及使用
1.1 list的介绍
list - C++ Reference
1. list 是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。 2. list 的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。 3. list 与 forward_list 非常相似:最主要的不同在于 forward_list 是单链表,只能朝前迭代,已让其更简单高效。 4. 与其他的序列式容器相比 (array , vector , deque) , list 通常在任意位置进行插入、移除元素的执行效率更好。 5. 与其他序列式容器相比, list 和 forward_list 最大的缺陷是不支持任意位置的随机访问,比如:要访问 list的第6 个元素,必须从已知的位置 ( 比如头部或者尾部 ) 迭代到该位置,在这段位置上迭代需要线性的时间开销;list 还需要一些额外的空间,以保存每个节点的相关联信息 ( 对于存储类型较小元素的大 list 来说这可能是一个重要的因素 1.2 list的使用
1.2.1 list的构造
| 构造函数 | 接口说明 |
| list() | 构造空的list |
| list(size_type n, const value_type&val = value_type()) | 构造的list中包含n个值为val的元素 |
| list(const list& x) | 拷贝构造函数 |
| list(Inputlterator fist,Inputlterator last) | 用(first,last)区间中的元素构造list |
//construting lists #include#include int main() { std::list
l1; //构造空的l1 std::list l2 (4,100);//12中放4个值为100的元素 std::list l3(l2.begin(), l2.end()); // 用l2的左闭右开区间构造L3 std::list l4(l3); //用l3拷贝构造l4 //以数组为迭代器区间构造l5 int array[] = { 16,2,77,29 }; std::list l5(array, array + sizeof(array) / sizeof(int)); //用迭代器方式打印l5中的元素 for (std::list ::iterator it = l5.begin(); it != l5.end(); it++) std::cout << *it << " "; std::cout << endl; //C++范围for的方式遍历 for(auto & e : l5) std::cout << e << " "; std::cout << endl; return 0; }
1.2.2 list iterator的使用
此处,大家可暂时将迭代器理解成一个指针,该指针指向list中的某个节点
| 函数声明 | 接口说明 |
| list::begin - C++ Reference+list::end - C++ Reference | 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 |
| list::rbegin - C++ Reference+list::rend - C++ Reference | 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的reverse_iterator,即begin位置 |
【注意】
1. begin 与 end 为正向迭代器,对迭代器执行 ++ 操作,迭代器向后移动 2. rbegin(end) 与 rend(begin) 为反向迭代器,对迭代器执行 ++ 操作,迭代器向前移动
#includeusing namespace std; #include void print_list(const list
& l) { //注意这里调用的是list的begin()const,返回list的const_iterator对象 for (list ::const_iterator it = l.begin(); it != l.end(); ++it) { cout << *it << " "; / template class ListIterator { typedef ListNode * PNode; typedef ListIterator Self; public: ListIterator(PNode pNode = nullptr) :_pNode(pNode) {} ListIterator(const Self& l) :_pNode(l._pNode) {} T& operator*() {return _pNode->val;} T* operator->(){return &(operator*())} Self& operator++() { _pNode = _pNode->_pNext; return *this; } Self operator++(int) { Self temp(*this); _pNode = _pNode->_pNext; return temp; } Self& operator--(); Self& operator--(int); bool operator!=(const Self& l) { return _pNode != l._pNode;} bool operator==(const Self& l) { return _pNode != l._pNode;} PNode_pNode; }; template class list { typedef ListNode Node; typedef Node* PNode; public: typedef ListIterator iterator; typedef ListIterator const_iterator; public: //List的构造 list() { CreateHead(); } list(int n, const T& value = T()) { CreateHead(); for (int i = 0; i < n; i++) push_back(value); } template list(Iterator first, Iterator last) { CreateHead(); while (first != last) { push_back(*first); ++first; } } list(const list & l) { CreateHead(); //用l中的元素构造临时的temp,然后与当前对象交换 list temp(l.cbegin(), l.cend()); this->swap(temp); } list & operator=(const list l) { this->swap(l); return *this; } ~list() { clear(); delete_pHead; _pHead = nullptr; } //List Itertor iterator begin() { return iterator(_pHead->_pNext); } itertor end() { return iterator(_pHead); } const_iterator begin() { return const_itertor(_pHead->_pNext); } const_iterator end() { return const_iterator(_pHead); } //List Capcity size_t size()const; bool empty()const; //List Access T& front(); const T& front()const; T& back(); const T& back()const; //List Modify void push_back(const T& val) { insert(begin(), val); } void pop_back() { erase(--end()); } void push_front(const T& val) { insert(begin(), val); } void pop_front() { erase(begin()); } //在pos位置前插入值为val的节点 iterator insert(iterator pos, const T& val) { PNode pNewNode = new Node(val); pNode pCur = pos._pNode; //先将新节点插入 pNewNode->_pPre = pCur->_pPre; pNewNode->_pNext = pCur; pNewNode->_pPre->_pNext = pNewNode; pCur->_pPre = pNewNode; return iterator(pNewNode); } //删除pos位置的节点,返回该节点的下一个位置 iterator erase(iterator pos) { //找到待删除的节点 PNode pDel = pos._pNode; PNode pRet = pDel->_pNext; //将该节点从链表拆下来并删除 pDel->_pPre->_pNext = pDel->_pNext; pDel->_pNext->_pPre = pDel->_pPre; delete pDel; return iterator(pRet); } void clear(); void swap(List & l); private: void CreateHead() { _pHead = new Node; _pHead->_pPre = _pHead; _pHead->_pNext = _pHead; } private: PNode _pHead; }; }
2.2 对模拟的bite::list进行测试
// 正向打印链表
template
void PrintList(const bite::list& l) {
auto it = l.cbegin();
while (it != l.cend())
{
cout << *it << " ";
++it;
}
cout << endl;
}
// 测试List的构造
void TestList1()
{
bite::list l1;
bite::list l2(10, 5);
PrintList(l2);
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
bite::list l3(array, array + sizeof(array) / sizeof(array[0]));
PrintList(l3);
bite::list l4(l3);
PrintList(l4);
l1 = l4;
PrintList(l1);
PrintListReverse(l1);
}
// PushBack()/PopBack()/PushFront()/PopFront()
void TestList2()
{
// 测试PushBack与PopBack
bite::list l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
PrintList(l);
l.pop_back();
l.pop_back();
PrintList(l);
l.pop_back();
cout << l.size() << endl;
// 测试PushFront与PopFront
l.push_front(1);
l.push_front(2);
l.push_front(3);
PrintList(l);
l.pop_front();
l.pop_front();
PrintList(l);
l.pop_front();
cout << l.size() << endl;
}
void TestList3()
{
int array[] = { 1, 2, 3, 4, 5 };
bite::list l(array, array + sizeof(array) / sizeof(array[0]));
auto pos = l.begin();
l.insert(l.begin(), 0);
PrintList(l);
++pos;
l.insert(pos, 2);
PrintList(l);
l.erase(l.begin());
l.erase(pos);
PrintList(l);
// pos指向的节点已经被删除,pos迭代器失效
cout << *pos << endl;
auto it = l.begin();
while (it != l.end())
{
it = l.erase(it);
}
cout << l.size() << endl;
}
3. list与vector的对比
vector与list都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性及应用场景不同,其主要不同如下:
3. list与vector的对比 vector与list都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性及应用场景不同,其主要不同如下:
| vector | list | |
| 底层结构 | 动态顺序表,一段连续空间 | 带头结点的双向循环链表 |
| 随机访问 | 支持随机访问,访问某个元素效率O(1) | 不支持随机访问,访问某个元素效率O(N) |
| 插入和删除 | 任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N) ,插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低 | 任意位置插入和删除效率高,不 需要搬移元素,时间复杂度O(1) |
| 空间利用率 | 底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高 | 底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低 |
| 迭 代 器 | 原生态指针 | 对原生态指针 ( 节点指针 ) 进行封装 |
| 迭 代 器 失 效 | 在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效 | 插入元素不会导致迭代器失效, 删除元素时,只会导致当前迭代 器失效,其他迭代器不受影响 |
| 使 用 场 景 | 需要高效存储,支持随机访问,不关心插入删除效率 | 大量插入和删除操作,不关心随 机访问 |



