目录
一、list的介绍
1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)
构造函数( (constructor)) |
接口说明 |
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 |
函数声明 |
接口说明 |
返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 |
|
返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置 |
注意:
1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
函数声明 |
接口说明 |
检测list是否为空,是返回true,否则返回false |
|
返回list中有效节点的个数 |
函数声明 |
接口说明 |
返回list的第一个节点中值的引用 |
|
返回list的最后一个节点中值的引用 |
函数声明 |
接口说明 |
在list首元素前插入值为val的元素 |
|
删除list中第一个元素 |
|
在list尾部插入值为val的元素 |
|
删除list中最后一个元素 |
|
在list position 位置中插入值为val的元素 |
|
删除list position位置的元素 |
|
交换两个list中的元素 |
|
清空list中的有效元素 |
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
list<int>::iterator it=lt.begin();
while(it!=lt.end())
{
cout<<*it<<" ";
++it;
}
cout<<endl;
it=lt.begin();
while(it!=lt.end())
{
(*it) *=2;
++it;
}
it=lt.begin();
while(it!=lt.end())
{
cout<<*it<<" ";
++it;
}
cout<<endl;
for(auto e: lt)
{
cout<<e<<" ";
}
cout<<endl;
lt.push_front(10);
lt.push_front(20);
lt.pop_back();
for(auto e: lt)
{
cout<<e<<" ";
}
cout<<endl;
};
迭代器失效问题
迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为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;
}
}
int main() {
TestListIterator1();
return 0;
}
// 改正
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);
}
}
int main() {
TestListIterator();
return 0;
}
void test_list2() {
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
auto pos=find(lt.begin(),lt.end(),3);
//找到了就插入
if(pos !=lt.end())
{
//pos是否会失效?不会
//相对位置关系是不会发生变化的,同时也不会产生野指针
//因为这是一个链表
lt.insert(pos,30);
}
for(auto e: lt)
{
cout<<e<<" ";
}
cout<<endl;
auto pos1=find(lt.begin(),lt.end(),3);
if(pos1!=lt.end())
{
//pos是否会失效?
lt.erase(pos1);
cout<<*pos1<<endl;
}
for(auto e: lt)
{
cout<<e<<" ";
}
cout<<endl;
}
remove的简单测试
remove会将list中所有指定的要删除的元素全部都删掉
void test_list3() {
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(3);
lt.push_back(5);
for(auto e: lt)
{
cout<<e<<" ";
}
cout<<endl;
//remove会将list中全部的3都去除掉
lt.remove(3);
for(auto e: lt)
{
cout<<e<<" ";
}
cout<<endl;
}
list中的sort问题
void test_list4() {
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(3);
lt.push_back(5);
//这个算法库中的sort是没办法用的
//sort(lt.begin(),lt.end());
//sort算法的迭代器用的是随机访问迭代器,链表的不是,因此不能用该算法
//使用list专门的sort是可以的,其采用归并的思路
lt.sort();
for(auto e: lt)
{
cout<<e<<" ";
}
cout<<endl;
}
//N个数据需要排序,vector+算法sort 还是list+sort?
//虽然快排和归并的效率是差不多的,但是vector支持随机访问,在大量的数据的情况下会比list+sort有很大的提升
//使用vector排完再拷贝回去都比list+sort快
// N个数据需要排序,vector+ 算法sort list+ sort
void test_op()
{
srand(time(0));
const int N = 100000;
vector<int> v;
v.reserve(N);
list<int> lt1;
list<int> lt2;
for (int i = 0; i < N; ++i)
{
auto e = rand();
//v.push_back(e);
lt1.push_back(e);
lt2.push_back(e);
}
// 拷贝到vector排序,排完以后再拷贝回来
int begin1 = clock();
for (auto e : lt1)
{
v.push_back(e);
}
sort(v.begin(), v.end());
size_t i = 0;
for (auto& e : lt1)
{
e = v[i++];
}
int end1 = clock();
int begin2 = clock();
// sort(lt.begin(), lt.end());
lt2.sort();
int end2 = clock();
printf("copy vector sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}
int main()
{
test_op();
return 0;
}
容器+sort比list+sort快出了不只一点点
二、list类的模拟实现
1.定义节点类,list类,迭代器类
①定义节点类
template<class T>
struct list_node
{
T _data;
list_node<T>* _next;
list_node<T>* _prev;
//构造函数
list_node(const T& x = T())
:_data(x)
, _next(nullptr)
, _prev(nullptr)
{}
};
②定义list类
template<class T>
class list
{
typedef list_node<T> Node;
public:
//采用复用的方式构建我们的普通迭代器和const迭代器
//第一个参数就是我们的list对象,后面两个参数用来区分是普通迭代器还是const迭代器
typedef __list_iterator<T, T&, T*> iterator;
//迭代器返回的是&T
typedef __list_iterator<T, const T&, const T*> const_iterator;
private:
Node* _head;
};
③定义迭代器类
// 像指针一样的对象
template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef list_node<T> Node;
//采用复用的方式构建我们的普通迭代器和const迭代器
//第一个参数就是我们的list对象,后面两个参数用来区分是普通迭代器还是const迭代器
typedef __list_iterator<T, Ref, Ptr> iterator;
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef ptrdiff_t difference_type;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
//不需用写迭代器的析构函数
//因为我们迭代器仅仅是用来遍历链表的,如果在遍历完成将这个结点给释放了
//那么我们的链表就断掉了!!!
//不需用写迭代器的拷贝构造
//因为默认的就是浅拷贝,也就是两个迭代器指向统一个结点,这也正是我们期望的
};
2.迭代器类中具体功能的定义
①迭代器所指向数据的比较
bool operator!=(const iterator& it) const
{
return _node != it._node;
}
bool operator==(const iterator& it) const
{
return _node == it._node;
}
②迭代器的++,--
用于迭代器的遍历
注意:链表结点的依次遍历和之前的数组是不同的!
// ++it
iterator& operator++()
{
_node = _node->_next;
return *this;
}
// it++
iterator operator++(int)
{
iterator tmp(*this);
_node = _node->_next;
return tmp;
}
// --it
iterator& operator--()
{
_node = _node->_prev;
return *this;
}
// it--
iterator operator--(int)
{
iterator tmp(*this);
_node = _node->_prev;
return tmp;
}
③迭代器->符号的重载
箭头是用来访问结构成员的。
Ref operator*()
{
return _node->_data;
}
//T* operator->()
Ptr operator->()
{
//&(_node->_data)
return &(operator*());
}
按照上面的代码,我们其实是需要输入it->->_a1来访问it迭代器中的_a1参数的,其中it->是调用了
it->会被转化成it.operator->(),也就是返回一个T*对象
而T*在会调用上面的函数
返回T*->_data
但是写两个箭头太难看了,编译器为了方便,就变成只输入一个->就可以了。
并且由于我们上面迭代器的定义方式,我们这里也并没有把是不是const属性给写死,这样下面函数中的Ref和Ptr会随着我们上面模板中的定义而随之改变,也就不用区分是不是const了。
3.list中具体功能的定义
①常量迭代器和迭代器的初始和结束位置的返回
const_iterator begin() const
{
return const_iterator(_head->_next);
}
const_iterator end() const
{
return const_iterator(_head);
}
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
②列表的构造函数
list()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
③列表的insert插入
iterator insert(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
// prev newnode cur
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
return iterator(newnode);
}
④列表的头插和尾插
void push_back(const T& x)
{
//可以手动重新写一个
//Node* tail = _head->_prev;
//Node* newnode = new Node(x);
_head tail newnode
//tail->_next = newnode;
//newnode->_prev = tail;
//newnode->_next = _head;
//_head->_prev = newnode;
//也可以复用insert
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
⑤列表的擦除函数erase
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
return iterator(next);
}
⑥列表的头插和头删
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
4.深浅拷贝的问题
如果我们给我们上面的代码添上析构函数,就会发生报错。因为我们两个list在发生拷贝的时候是浅拷贝,对于同一个结点会释放两次,所以就会发生报错。这里我们就需要将我们的浅拷贝调整为深拷贝
析构函数
~list()
{
clear();
delete _head;
_head= nullptr;
}
在析构之前我们最好提供一个clear函数
clear仅仅是清除数据,但是头结点是不清除的的
clear函数
void clear()
{
iterator it=begin();
while(it!=end())
{
//返回下一个位置的迭代器,防止迭代器失效
it=erase(it);
}
}
初始化函数
这里我们单独将初始化函数定义出来,然后其他的代码复用初始化函数
//创建并初始化哨兵位头结点
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
//lt2(lt1)
list(const list<T>& lt)
{
//现代写法,最好先初始化一下
empty_init();
list<T> tmp(lt.begin(),lt.end());
swap(tmp);
}
void swap(list<T> lt)
{
std::swap(_head,lt._head);
}
list()
{
empty_init();
}
定义深拷贝的拷贝函数
template<class InputIterator>
list(InputIterator first,InputIterator last)
{
empty_init();
while(first!=last)
{
//将节点中的内容一个一个拷贝
push_back(*first);
++first;
}
}
定义赋值拷贝
//lt1=lt3
list<T>& operator=(list<T> lt)
{
swap(lt);
return *this;
}
测试代码
void test_list4()
{
list<int> lt;
lt.push_back(1);
lt.push_back(3);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(3);
lt.push_back(3);
lt.push_back(5);
// list<int>copy(lt);
list<int> copy=lt;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
list<int>::iterator it =copy.begin();
while(it!=copy.end())
{
(*it)*=2;
it++;
}
cout<<endl;
// cout<<(*copy.begin()*=2)<<endl;
for (auto e : copy)
{
cout << e << " ";
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
我们观察到修改拷贝的list,我们原来的list并不会发生变化
5.细节问题
在类外面必须带上模板参数,比方说下面这里
在类里面编译器会做优化,比方说这里的<T>其实不写也可以,但是写上最好
可以看到编译器并没有报错。
三、头文件代码汇总
#ifndef LIST_TEST2_LIST_H
#define LIST_TEST2_LIST_H
#pragma once
#include <assert.h>
namespace zhuyuan
{
template<class T>
struct list_node
{
T _data;
list_node<T>* _next;
list_node<T>* _prev;
list_node(const T& x = T())
:_data(x)
, _next(nullptr)
, _prev(nullptr)
{}
};
// typedef __list_iterator<T, T&, T*> iterator;
// typedef __list_iterator<T, const T&, const T*> const_iterator;
// 像指针一样的对象
template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef list_node<T> Node;
typedef __list_iterator<T, Ref, Ptr> iterator;
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef ptrdiff_t difference_type;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
bool operator!=(const iterator& it) const
{
return _node != it._node;
}
bool operator==(const iterator& it) const
{
return _node == it._node;
}
// *it it.operator*()
// const T& operator*()
// T& operator*()
Ref operator*()
{
return _node->_data;
}
//T* operator->()
Ptr operator->()
{
return &(operator*());
}
// ++it
iterator& operator++()
{
_node = _node->_next;
return *this;
}
// it++
iterator operator++(int)
{
iterator tmp(*this);
_node = _node->_next;
return tmp;
}
// --it
iterator& operator--()
{
_node = _node->_prev;
return *this;
}
// it--
iterator operator--(int)
{
iterator tmp(*this);
_node = _node->_prev;
return tmp;
}
};
template<class T>
class list
{
typedef list_node<T> Node;
public:
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
const_iterator begin() const
{
return const_iterator(_head->_next);
}
const_iterator end() const
{
return const_iterator(_head);
}
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
list()
{
empty_init();
}
template<class InputIterator>
list(InputIterator first,InputIterator last)
{
empty_init();
while(first!=last)
{
//将节点中的内容一个一个拷贝
push_back(*first);
++first;
}
}
//创建并初始化哨兵位头结点
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
//lt2(lt1)
list(const list& lt)
{
//现代写法,最好先初始化一下
empty_init();
list<T> tmp(lt.begin(),lt.end());
swap(tmp);
}
void swap(list<T> lt)
{
std::swap(_head,lt._head);
}
~list()
{
clear();
delete _head;
_head= nullptr;
}
void clear()
{
iterator it=begin();
while(it!=end())
{
//返回下一个位置的迭代器,防止迭代器失效
it=erase(it);
}
}
//lt1=lt3
list<T>& operator=(list<T> lt)
{
swap(lt);
return *this;
}
void push_back(const T& x)
{
//Node* tail = _head->_prev;
//Node* newnode = new Node(x);
_head tail newnode
//tail->_next = newnode;
//newnode->_prev = tail;
//newnode->_next = _head;
//_head->_prev = newnode;
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator insert(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
// prev newnode cur
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
return iterator(newnode);
}
void pop_back()
{
erase(--end());
}
void pop_front()
{
erase(begin());
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
return iterator(next);
}
private:
Node* _head;
};
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
it = lt.begin();
while (it != lt.end())
{
*it *= 2;
++it;
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
struct Pos
{
int _a1;
int _a2;
Pos(int a1 = 0, int a2 = 0)
:_a1(a1)
,_a2(a2)
{}
};
void test_list2()
{
int x = 10;
int* p1 = &x;
cout << *p1 << endl;
Pos aa;
Pos* p2 = &aa;
p2->_a1;
p2->_a2;
list<Pos> lt;
lt.push_back(Pos(10, 20));
lt.push_back(Pos(10, 21));
list<Pos>::iterator it = lt.begin();
while (it != lt.end())
{
//cout << (*it)._a1 << ":" << (*it)._a2 << endl;
cout << it->_a1 << ":" << it->_a2 << endl;
++it;
}
cout << endl;
}
void Func(const list<int>& l)
{
list<int>::const_iterator it = l.begin();
while (it != l.end())
{
//*it = 10;
cout << *it << " ";
++it;
}
cout << endl;
}
void test_list3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
Func(lt);
}
void test_list4()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
it = lt.begin();
while (it != lt.end())
{
*it *= 2;
++it;
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.push_front(10);
lt.push_front(20);
lt.push_front(30);
lt.push_front(40);
lt.pop_back();
lt.pop_back();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
auto pos = find(lt.begin(), lt.end(), 4);
if (pos != lt.end())
{
// pos是否会失效?不会
lt.insert(pos, 40);
//lt.insert(pos, 30);
*pos *= 100;
}
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.clear();
}
}
#endif //LIST_TEST2_LIST_H
四、测试代码汇总
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <time.h>
using namespace std;
#include "list.h"
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
it = lt.begin();
while (it != lt.end())
{
*it *= 2;
++it;
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.push_front(10);
lt.push_front(20);
lt.push_front(30);
lt.push_front(40);
lt.pop_back();
lt.pop_back();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
void test_list2()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
auto pos = find(lt.begin(), lt.end(), 3);
if (pos != lt.end())
{
// pos是否会失效?不会
lt.insert(pos, 30);
//lt.insert(pos, 30);
*pos *= 100;
}
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
pos = find(lt.begin(), lt.end(), 4);
if (pos != lt.end())
{
// pos是否会失效?会
lt.erase(pos);
// cout << *pos << endl;
}
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
void test_list3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(3);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(3);
lt.push_back(3);
lt.push_back(5);
/*for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.remove(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;*/
// sort(lt.begin(), lt.end());
lt.sort();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
// N个数据需要排序,vector+ 算法sort list+ sort
void test_op()
{
srand(time(0));
const int N = 100000;
vector<int> v;
v.reserve(N);
list<int> lt1;
list<int> lt2;
for (int i = 0; i < N; ++i)
{
auto e = rand();
//v.push_back(e);
lt1.push_back(e);
lt2.push_back(e);
}
// 拷贝到vector排序,排完以后再拷贝回来
int begin1 = clock();
for (auto e : lt1)
{
v.push_back(e);
}
sort(v.begin(), v.end());
size_t i = 0;
for (auto& e : lt1)
{
e = v[i++];
}
int end1 = clock();
int begin2 = clock();
// sort(lt.begin(), lt.end());
lt2.sort();
int end2 = clock();
printf("copy vector sort:%d\n", end1 - begin1);
printf("list sort:%d\n", end2 - begin2);
}
void test_list4()
{
list<int> lt;
lt.push_back(1);
lt.push_back(3);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(3);
lt.push_back(3);
lt.push_back(5);
// list<int>copy(lt);
list<int> copy=lt;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
list<int>::iterator it =copy.begin();
while(it!=copy.end())
{
(*it)*=2;
it++;
}
cout<<endl;
// cout<<(*copy.begin()*=2)<<endl;
for (auto e : copy)
{
cout << e << " ";
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
test_list4();
// test_op();
// bit::test_list4();
//
return 0;
}