目录
类模板:C++模板初阶
问题1:原生指针不能充当迭代器(原生指针是天然的迭代器的前提是空间连续)
原因:原生指针指向的是连续空间的情况下才可以充当迭代器
问题2:对结点的原生指针的解引用得不到当前所在结点的数据
难点:Node、iterator、list三个类的间接嵌套使用
- Node、iterator、list都是一个类
- Node类负责表示的单个结点的结构,并提供相关的方法来操作单个结点
- list类负责管理所有结点间的关系及提供对外接口来让用户操作整个链表
- iterator类负责实现封装原生指针和实现迭代器需要的方法
template <class T>
struct ListNode
{
ListNode<T>* _next;//结点的后继指针
ListNode<T>* _prev;//结点的前驱指针
T _data;//结点中存放的数据
ListNode(const T& x = T())//构造Node类类型的对象(一个结点对象)
:_next(nullptr)//未传入指定数据,x就会等于该匿名对象
,_prev(nullptr)//传入指定数据,x会等于那个指定的数据,T()不起作用
,_data(x)
{}
};
1、在定义一个类时,如果类中的数据可以公用就选struct,需要保护一部分就用class,结点中的数据和前驱后继指针应该都能被访问到,所以可以直接选用struct
2、T()是一个
T类型
匿名对象,在构造结点时未传入有效数据,x就会给予T()进行初始化:
若T
是内置类型(如 int、float、指针等),将x将被初始化为0、0.0或nullptr等默认值
若T
是自定义类类型,则将调用该类的默认构造函数来创建一个匿名对象
template<class T>
struct ListIterator
{
typedef ListNode<T> Node;//此时迭代器类可以使用结点类
typedef ListIterator<T> iterator;//将迭代器类重名名为iterator
Node* _node;
ListIterator(Node* node)//用_node封装原生指针,_node会被传入的原生指针初始化
:_node(node) //_node = _head->_next
{} //_node就相当于原生指针
};//记得类模板后的;
//*it
T& operator*()//传引用返回避免了拷贝,且该数据可读可修改
{
return _node->_data;//返回_node指向的结点对象中存放的数据_data
}
//a->b
T* operator->()//返回值为T*,T*表示T类型的指针
{
return &_node->_data;//获取T类类型对象的地址,将它交给一个指向T类类型的对象的“匿名”指针,该指针的类型是T*,之后利用该指针去访问该对象中的成员变量的值
}
问题:为什么要返回_data的地址而不是返回_data?
答:用->访问对象中的成员,左操作数是指向该对象的指针而不是该对象本身,右操作数是要访问对象的成员(A* ptr = &aa1,ptr->_a1,ptr存放的是该对象的地址,此后就可以用ptr访问_a1了),返回的地址不用一个有名的指针承接,直接接->,此时返回的内容就类似T*类型的匿名指针( (指向对象的匿名指针)->对象的成员 )
问题:返回值类型可不可以是T&?
答:不可以,T* + 返回的地址 = 一个T*类型的指向返回地址的匿名指针,返回的地址被存放在了一个匿名指针中,T& + 返回的地址 = 未定义行为(函数返回一个对象的地址,则该函数的返回值类型必须是 该对象的类型*)
//前置++,返回++后的值
iterator& operator++()//传引用返回
{
_node = _node->_next;//更新迭代器
return *this;//返回更新结果
}
//后置++,返回++前的值
iterator operator++(int)
{
iterator tmp(*this);//拷贝构造一个临时iterator类类型的对象存放当前迭代器的位置
_node = _node->_next;//更新迭代器
return tmp;//返回更新前结果
}
//前置--,返回--后的值
iterator& operator--()
{
_node = _node->_prev;
return *this;
}
//后置--,返回--前的值
iterator operator--(int)
{
iterator tmp(*this);
_node = _node->_prev;
return tmp;
}
//不等于,返回判断结果
bool operator!=(const iterator& it)//等价于(const iterator& this,const iterator& it)
{ //传俩迭代器
return _node != it._node;//等价于this->_node != it.node
}//当前迭代器对象中的_node是否等于另一个迭代器对象中的_node
//等于
bool operator==(const iterator& it)
{
return _node == it._node;
}
1、普通迭代器是迭代器本身可以修改,迭代器指向的内容也可以修改
2、const迭代器是迭代器本身可以修改,迭代器指向的内容不可以修改
3、同一命名空间下,多个类之间可以通过typedef的方式使用其他类的内容
template<class T>
class list
{
public:
typedef ListNode<T> Node;//此时list类可以使用结点类
typedef ListIterator<T> iterator;//此时list类可以使用迭代器类
//初始化一个哨兵位
void empty_init()
{
_head = new Node();//_head指向一个在堆上构造的Node类类型的匿名结点对象(空结点)
//不加new的话链表的结点空间在栈上开辟,而不是堆上
//连接链表首尾
_head->_next = _head;//_head访问结点对象中的_next指针,并将其中存放的地址是变为_head
_head->_prev = _head;//_head访问结点对象中的_prev指针,并将其中存放的地址是变为_head
//链表元素个数为0
_size = 0;
}
list()//构造函数
{
empty_init();//调用empty_init函数即可
}
private:
Node* _head;//_head用于指向链表的哨兵位结点,结点类实例化出的结点对象的类型是Node*,故
size_t _size;//_head也是Node*型
};
//在指定位置前插入新结点
void insert(iterator pos, const T& val)//pos是迭代器类型的对象,它的成员_node存放指定位置的地址
{
Node* newnode = new Node(val);//new一个新结点对象
Node* cur = pos._node;//获取指定位置的地址
Node* prev = cur->_prev;//prev保存当前结点的前一个结点信息
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
_size++;
}
//删除指定位置的结点
iterator erase(iterator pos)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
_size--;
return iterator(next);//删除指定位置的结点后要更新迭代器中存放的地址信息,否则迭代器失效
}
iterator begin()//返回迭代器类型的对象
{
//return iterator(_head->_next);
//原生指针_head不能直接当作迭代器使用,_head++得不到下一个结点对象的地址
//用_head->_next构造一个迭代器类类型的对象,并初始化该对象中的_node成员变量
//_head->_next={xxx}、_node={xxx},起始时二者中存放的内容一样
//构造好的迭代器类类型的对象会被返回给iterator类类型的变量it
//it++会调用迭代器类类型对象中的++重载函数:_node = _node->_next; return *this,
//而不是return _head然后it++就会得到下一个对象的地址
return _head->_next;//返回类型的迭代器类类型的对象,_head->_next会调用该类的构造函数构造一个对象出来,运用了单参数构造函数会发生隐式类型转换(Node* -> iterator)
}
iterator end()
{
return _head;//同理
}
/*//初始尾插函数
void push_back(const T& x)
{
//Node* newnode = new Node(x);//newnode指向一个在堆上构造被x初始化的Node类类型的匿名结点对象,故newnode的类型是Node*
//Node* tail = _head->_prev;//tail指向哨兵位的前驱结点(尾结点),工具人,用完就可以扔,
//_prev存放的是前驱结点对象的地址,结点类型是Node,故tail是Node*
//交换指向
//tail->_next = newnode;
//newnode->_prev = tail;
//newnode->_next = _head;
//_head->_prev = newnode;
}*/
//复用接口后的尾插函数
void push_back(const T& x)//调用end()函数,end函数返回链表的尾部迭代器
{
insert(end(), x);
}
//头插函数
void push_front(const T& x)
{
insert(begin(), x);/返回链表头部迭代器
}
//尾删函数
void pop_back()
{
erase(--end());//迭代器--,调用--重载函数
}
//头删函数
void pop_front()
{
erase(begin());
}
//清除链表存放有效数据的结点,因为迭代器传入的是_head->_next而不是_head
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);//防止迭代器失效问题,erase会返回迭代器中最新存放的地址信息,更新it
}
}
//销毁链表
~list()
{
clear();
delete _head;//删除_head指向的空间(哨兵位),因为之前new了一个哨兵位空间
_head = nullptr;//将_head置空
}
//a(b)
//拷贝构造函数
list(const list<T>& lt)//const修饰lt(const list<T>& this,const list<T>& lt)
{
empty_init();//创建哨兵位
for (auto& e : lt)//虽然这里是&可以对lt中的结点对象进行修改,但本质目的还是为了防止拷贝
{
push_back(e);//push_back(this,e),e此时就是一个结点对象的引用
}
}
//自定义交换函数
void swap(list<T>& lt)//传引用传参直接在原对象基础上修改,(list<T>& this,list<T>& lt)
{
std::swap(_head, lt,_head);//借用算法库提供的swap函数
std::swap(_size, lt._size);
}
//lt1 = lt3
//赋值运算符的重载
list<T>& operator=(list<T> lt)//右操作对象拷贝构造一个临时对象,传引用返回返回的是交换后的对象
{
swap(lt);//(this,lt)
return *this;//返回交换后得到的对象
}
//求大小
size_t size()const
{
return _size;
}
//判空
bool empty()
{
return _size == 0;
}
//头尾插、头尾删、指定位置前插、删除指定位置并更新迭代器,迭代器的基础实现
void test_list1()
{
list<int> lt;/
lt.push_back(1);
lt.push_back(2);
list<int>::iterator it = lt.begin();//it是迭代器类型的变量
//cout << typeid(it).name() << endl;//it的类型是struct bit::ListIterator<int>
while (it != lt.end())
{
cout << *it << " "; //*it == it.operator*()
++it; //++it == it.operator++(int)
}
cout << endl;
lt.push_front(3);
lt.push_front(4);
for (auto& e : lt)//迭代器写好后用范围for,编译器自动调用迭代器
{
cout << e << " ";
}
cout << endl;
}
//迭代器的进阶理解与补充实现
void test_list2()
{
list<A> lt;
A aa1(1, 1);
A aa2 = { 1,1 };//C++11提供的多参数的构造函数的隐式类型转换,initializer_list类型变为A类型
lt.push_back(aa1);//使用有名对象尾插
lt.push_back(aa2);
lt.push_back(A(2,2));//匿构造名对象并尾插
lt.push_back({3,3});//多参数的构造函数的隐式类型转换
lt.push_back({4,4});
list<A>::iterator it = lt.begin();
while (it != lt.end())
{
//cout << *it << endl;*it得到it中存放的A类类型对象,自定义类型数据不支持流插入
//获取A类类型对象中存放的成员变量的值
//方法一:*.
//cout << (*it)._a1 << ":" << (*it)._a2 << endl;
//++it;//指向链表中存放的下一个A类类型的对象
//方法二:->
cout << it->_a1 << ":" << it->_a2 << endl;
++it;
//等价于:cout << it->->_a1 << ":" << it->->_a2 << endl;//编译器为了可读性省略了一个->
//等价于:cout << it.operator->()->_a1 << ":" << it.operator->()->_a2 << endl;
//it.operator->()->_a1 : (A* "匿名"指针) -> _a1
}
cout << endl;
}
测试函数3
///迭代器的最终实现
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);
PrintList(lt);
list<int> lt1(lt);
PrintList(lt1);
}
1、需要析构的类,一般需要自己写深拷贝
2、不需要析构的类,一般不需要自己写深拷贝,默认浅拷贝即可
3、引用可以初始化另一个变量
4、 ->是成员访问运算符用于访问对象中的成员(函数或变量),左操作数是一个指向要访问对象的指针,右操作数是该对象成员(函数或变量),“指针->成员”在程序执行时会被编译器自动转换为(*指针).成员,即对象.成员,因为->重载函数要返回的是(A* 对象的地址)相当于一个匿名指针
//打印函数
void PrintList(const list<int>& clt)//传入的是被const修饰的clt(只读)
{
//list<int>::iterator it = clt.begin();//错误,普通迭代器对链表对象的访问会造成权限放大
list<int>::const_iterator it = clt.begin();//const修饰的链表,同样的要用const修饰的迭代器
while (it != clt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
问题1:对于const修饰的对象,普通迭代器会导致权限的放大,怎么办?
答:重命名一个const_iterator的迭代器对象,使用时由编译器决定实例化哪个迭代器对象
template<class T>
struct ListIterator
{
typedef ListNode<T> Node;//此时迭代器类可以使用结点类
typedef ListIterator<T> const_iterator;//为当前类模板起别名,const_iterator
Node* _node;
ListIterator(Node* node)
:_node(node)
{}
//*
const T& operator*()//返回值被const修饰
{
return _node->_data;
}
//->
const T* operator->()//返回值被const修饰
{
return &_node->_data;
}
//前置++,返回++后的值
const_iterator& operator++()
{
_node = _node->_next;
return *this;
}
//后置++,返回++前的值
const_iterator operator++(int)
{
iterator tmp(*this);
_node = _node->_next;
return tmp;
}
//前置--,返回--后的值
const_iterator& operator--()
{
_node = _node->_prev;
return *this;
}
//后置--,返回--前的值
const_iterator operator--(int)
{
iterator tmp(*this);
_node = _node->_prev;
return tmp;
}
//不等于,返回判断结果
bool operator!=(const const_iterator & it)
{
return _node != it._node;
}
//等于
bool operator==(const const_iterator & it)
{
return _node == it._node;
}
};
注意事项:const_iterator而不是const iterator,前者是别名,后者表示迭代器本身不能修改,const迭代器要的是迭代器指向的对象不能被修改而不是迭代器本身不能被修改
问题2:普通迭代器和const迭代器的代码差别不大,能不能综合一下?
答:还是利用类模板
template<class T, class Ref, class Ptr>
struct ListIterator
{
typedef ListNode<T> Node;
typedef ListIterator<T, Ref, Ptr> Self;
Node* _node;
ListIterator(Node* node)
:_node(node)
{}
// *it
//T& operator*()
Ref operator*()
{
return _node->_data;
}
// it->
//T* operator->()
Ptr operator->()
{
return &_node->_data;
}
// ++it
Self& operator++()
{
_node = _node->_next;
return *this;
}
Self operator++(int)
{
Self tmp(*this);
_node = _node->_next;
return tmp;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
Self operator--(int)
{
Self tmp(*this);
_node = _node->_prev;
return tmp;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
bool operator==(const Self& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef ListNode<T> Node;
public:
//typedef ListIterator<T> iterator;
//typedef ListConstIterator<T> const_iterator;
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T*> const_iterator;
};
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
namespace bit
{
/* Node、iterator、list都是一个类
Node类负责表示的单个结点的结构,并提供相关的方法来操作单个结点
list类负责管理所有结点间的关系及提供对外接口来让用户操作整个链表
iterator类负责实现封装原生指针和实现迭代器需要的方法 */
template <class T>
struct ListNode
{
ListNode<T>* _next;//结点的后继指针
ListNode<T>* _prev;//结点的前驱指针
T _data;//结点中存放的数据
ListNode(const T& x = T())//构造Node类类型的对象(一个结点对象)
:_next(nullptr)//未传入指定数据,x就会等于该匿名对象
, _prev(nullptr)//传入指定数据,x会等于那个指定的数据,T()不起作用
, _data(x)
{}
};
template<class T, class Ref, class Ptr>
struct ListIterator
{
typedef ListNode<T> Node;
typedef ListIterator<T, Ref, Ptr> iterator;
Node* _node;
ListIterator(Node* node)
:_node(node)
{}
// *it
//T& operator*()
Ref operator*()
{
return _node->_data;
}
// it->
//T* operator->()
Ptr operator->()
{
return &_node->_data;
}
// ++it
iterator& operator++()
{
_node = _node->_next;
return *this;
}
iterator operator++(int)
{
iterator tmp(*this);
_node = _node->_next;
return tmp;
}
iterator& operator--()
{
_node = _node->_prev;
return *this;
}
iterator operator--(int)
{
iterator tmp(*this);
_node = _node->_prev;
return tmp;
}
bool operator!=(const iterator& it)
{
return _node != it._node;
}
bool operator==(const iterator& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef ListNode<T> Node;
public:
//typedef ListIterator<T> iterator;
//typedef ListConstIterator<T> const_iterator;
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T*> const_iterator;
//初始化一个哨兵位
void empty_init()
{
_head = new Node();//_head指向一个在堆上构造的Node类类型的匿名结点对象(空结点)
//不加new的话链表的结点空间在栈上开辟,而不是堆上
//连接链表首尾
_head->_next = _head;//_head访问结点对象中的_next指针,并将其中存放的地址是变为_head
_head->_prev = _head;//_head访问结点对象中的_prev指针,并将其中存放的地址是变为_head
//链表元素个数为0
_size = 0;
}
list()//构造函数
{
empty_init();//调用empty_init函数即可
}
//在指定位置前插入新结点
void insert(iterator pos, const T& val)//pos是迭代器类型的对象,它的成员_node存放指定位置的地址
{
Node* newnode = new Node(val);//new一个新结点对象
Node* cur = pos._node;//获取指定位置的地址
Node* prev = cur->_prev;//prev保存当前结点的前一个结点信息
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
_size++;
}
//删除指定位置的结点
iterator erase(iterator pos)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
_size--;
return iterator(next);//删除指定位置的结点后要更新迭代器中存放的地址信息,否则迭代器失效
}
iterator begin()//返回迭代器类型的对象
{
//return iterator(_head->_next);
//原生指针_head不能直接当作迭代器使用,_head++得不到下一个结点对象的地址
//用_head->_next构造一个迭代器类类型的对象,并初始化该对象中的_node成员变量
//_head->_next={xxx}、_node={xxx},起始时二者中存放的内容一样
//构造好的迭代器类类型的对象会被返回给iterator类类型的变量it
//it++会调用迭代器类类型对象中的++重载函数:_node = _node->_next; return *this,
//而不是return _head然后it++就会得到下一个对象的地址
return _head->_next;//返回类型的迭代器类类型的对象,_head->_next会调用该类的构造函数构造一个对象出来,运用了单参数构造函数会发生隐式类型转换(Node* -> iterator)
}
iterator end()
{
return _head;//同理
}
const_iterator begin()const
{
return _head->_next;
}
const_iterator end()const
{
return _head;
}
初始尾插函数
//void push_back(const T& x)
//{
// //Node* newnode = new Node(x);//newnode指向一个在堆上构造被x初始化的Node类类型的匿名结点对象,故newnode的类型是Node*
// //Node* tail = _head->_prev;//tail指向哨兵位的前驱结点(尾结点),工具人,用完就可以扔,
// //_prev存放的是前驱结点对象的地址,结点类型是Node,故tail是Node*
// //交换指向
// //tail->_next = newnode;
// //newnode->_prev = tail;
// //newnode->_next = _head;
// //_head->_prev = newnode;
//}
//复用接口后的尾插函数
void push_back(const T& x)//调用end()函数,end函数返回链表的尾部迭代器
{
insert(end(), x);
}
//头插函数
void push_front(const T& x)
{
insert(begin(), x); // 返回链表头部迭代器
}
//尾删函数
void pop_back()
{
erase(--end());//迭代器--,调用--重载函数
}
//头删函数
void pop_front()
{
erase(begin());
}
//清除链表存放有效数据的结点,因为迭代器传入的是_head->_next而不是_head
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);//防止迭代器失效问题,erase会返回迭代器中最新存放的地址信息,更新it
}
}
//销毁链表
~list()
{
clear();
delete _head;//删除_head指向的空间(哨兵位),因为之前new了一个哨兵位空间
_head = nullptr;//将_head置空
}
//a(b)
//拷贝构造函数
list(const list<T>& lt)//const修饰lt(const list<T>& this,const list<T>& lt)
{
empty_init();//创建哨兵位
for (auto& e : lt)//虽然这里是&可以对lt中的结点对象进行修改,但本质目的还是为了防止拷贝
{
push_back(e);//push_back(this,e),e此时就是一个结点对象的引用
}
}
//自定义交换函数
void swap(list<T>& lt)//传引用传参直接在原对象基础上修改,(list<T>& this,list<T>& lt)
{
std::swap(_head, lt, _head);//借用算法库提供的swap函数
std::swap(_size, lt._size);
}
//lt1 = lt3
//赋值运算符的重载
list<T>& operator=(list<T> lt)//右操作对象拷贝构造一个临时对象,传引用返回返回的是交换后的对象
{
swap(lt);//(this,lt)
return *this;//返回交换后得到的对象
}
//求大小
size_t size()const
{
return _size;
}
//判空
bool empty()
{
return _size == 0;
}
private:
Node* _head;//_head用于指向链表的哨兵位结点,结点类实例化出的结点对象的类型是Node*,故
size_t _size;//_head也是Node*型
};
//头尾插、头尾删、指定位置前插、删除指定位置并更新迭代器,迭代器的基础实现
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
list<int>::iterator it = lt.begin();//it是迭代器类型的变量
//cout << typeid(it).name() << endl;//it的类型是struct bit::ListIterator<int>
while (it != lt.end())
{
cout << *it << " "; //*it == it.operator*()
++it; //++it == it.operator++(int)
}
cout << endl;
lt.push_front(3);
lt.push_front(4);
for (auto& e : lt)//迭代器写好后用范围for,编译器自动调用迭代器
{
cout << e << " ";
}
cout << endl;
}
struct A
{
int _a1;
int _a2;
A(int a1 = 0, int a2 = 0)
:_a1(a1)
, _a2(a2)
{}
};
//迭代器的进阶理解与补充实现
void test_list2()
{
list<A> lt;
A aa1(1, 1);
A aa2 = { 1,1 };//C++11提供的多参数的构造函数的隐式类型转换,initializer_list类型变为A类型
lt.push_back(aa1);//使用有名对象尾插
lt.push_back(aa2);
lt.push_back(A(2, 2));//匿构造名对象并尾插
lt.push_back({ 3,3 });//多参数的构造函数的隐式类型转换
lt.push_back({ 4,4 });
list<A>::iterator it = lt.begin();
while (it != lt.end())
{
//cout << *it << endl;*it得到it中存放的A类类型对象,自定义类型数据不支持流插入
//获取A类类型对象中存放的成员变量的值
//方法一:*.
//cout << (*it)._a1 << ":" << (*it)._a2 << endl;
//++it;//指向链表中存放的下一个A类类型的对象
//方法二:->
cout << it->_a1 << ":" << it->_a2 << endl;
++it;
//等价于:cout << it->->_a1 << ":" << it->->_a2 << endl;//编译器为了可读性省略了一个->
//等价于:cout << it.operator->()->_a1 << ":" << it.operator->()->_a2 << endl;
//it.operator->()->_a1 : (A* "匿名"指针) -> _a1
}
cout << endl;
}
//打印函数
void PrintList(const list<int>& clt)//传入的是被const修饰的clt(只读)
{
//list<int>::iterator it = clt.begin();//错误,普通迭代器对链表对象的访问会造成权限放大
list<int>::const_iterator it = clt.begin();//const修饰的链表,同样的要用const修饰的迭代器
while (it != clt.end())
{
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);
PrintList(lt);
list<int> lt1(lt);
PrintList(lt1);
}
}
#include "list.h"
int main()
{
//bit::test_list1();
//bit::test_list2();
//bit::test_list3();
return 0;
}
~over~
更多【c++-C++的list类(二):list类的模拟实现和迭代器模板】相关视频教程:www.yxfzedu.com