一、迭代器
1.迭代器
定义:迭代器是一种检查容器内元素并遍历元素的数据类型。迭代器提供对一个容器中的对象的访问方法,并且定义了容器中对象的范围。
2.失效迭代器
//失效迭代器
void erase(iterator _F, iterator _L)
{
for (; _F != _L; )
{
erase(_F++);
}
}
int main()
{
yhp::list<Object> ilst;
Object a(10);
ilist.push_back(a);
yhp::list<Object>::iterator it = ilist.begin();//迭代器指向链表第一个数据节点(失效迭代器)
//int*p=(int*)malloc(sizeof(int));*p=100;free(p);cout<<*p<<endl;//失效指针
cout << it->Value() << endl;
ilist.eraser(it);
cout << it->Value() << endl;
return 0;
}
二、
1.前置++和后置++区别
//内置类型先取值,后++;自己定义的类型,先++后取值。自己设计的类型系统调用方式和函数相同,内置类型按照运算符方式调用
示例代码
#include<iostream>
using namespace std;
class Int
{
int value;
public:
Int(int x=0) :value(x) {}
Int operator++()//前置++
{
value += 1;
return *this;
}
Int operator++(int)//后置++
{
Int tmp = *this;
++* this;
return tmp;
}
void Print() const
{
cout << value << endl;
}
};
void fun(Int x)
{
x.Print();
}
int main()
{
Int a = 10;
fun(a++);
return 0;
}
2.删除节点
iterator erase(iterator _P)
{
_Nodeptr _S = _P.Mynode();
_Acc::_Next(_Acc::_Prev(_S)) = _Acc::_Next(_S);
_Acc::_Prev(_Acc::_Next(_S)) = _Acc::_Prev(_S);
(&_Acc::_Value(_S))->~_Ty;//调动析构函数
_Freenode(_S);
return _P;
}
3.赋值运算符的重载
不需要判断_Head指针是否为空
list<int> alist;
list<int> blist;
//alist->_Head,blist->_Head
list& operator=(const list _X)
{
if (this == &_X)
{
return *this;
}
iterator _F1 = begin();
iterator _L1 = end();
const_iterator _F2 = _X.begin();//末尾迭代器
const_iterator _L2 = _X.end();
for (; _F1 != _L1 && _F2 != _L2; ++_F1, ++_F2)
{
*_F1 = *_F2;
}
erase(_F1, _L1);
insert(_L1, _F2, _L2);//赋值的链表小于要插入的链表个数,系统直接加入新结点,把剩余数据补充进去
return *this;
}
三、List
yhp_list.h
#ifndef YHP_LIST_H
#define YHP_LIST_H
// reference;
namespace yhp
{
template<class T>
void Swap(T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
template<class _Ty>
class list
{
protected:
struct _Node;//声明结构体
typedef struct _Node* _Nodeptr;//声明结构体类型指针
struct _Node
{
_Nodeptr _Prev, _Next;//前驱,后继
_Ty _Value;//数据域(按照值的引用返回)
};
struct _Acc;
struct _Acc
{
typedef struct _Node*& _Nodepref;//指针的引用类型
typedef _Ty& _Vref;//值引用类型
static _Vref _Value(_Nodeptr _P)
{
return (*_P)._Value;
}
static _Nodepref _Prev(_Nodeptr _P)
{
return (*_P)._Prev;
}
static _Nodepref _Next(_Nodeptr _P)
{
return (*_P)._Next; // return _P->_Next;
}
};
public:
typedef _Ty value_type;//值类型
typedef _Ty& reference;//引用类型
typedef const _Ty& const_reference;//常引用类型
typedef _Ty* pointer;//指针类型
typedef const _Ty* const_pointer;//常指针类型
typedef size_t size_type;//大小类型
typedef int difference_type;//差值类型
public:
class const_iterator
{
protected:
_Nodeptr _Ptr;
public:
const_iterator(_Nodeptr _P = NULL) :_Ptr(_P) {}
const_reference operator*() const
{
return _Acc::_Value(_Ptr);
}
const_pointer operator->() const
{
return &**this;
}
//可以重载自增自减运算符
const_iterator& operator++()
{
_Ptr = _Acc::_Next(_Ptr);
return *this;
}
const_iterator operator++(int)
{
const_iterator tmp = *this;
++* this;
return tmp;
}
const_iterator& operator--()
{
_Ptr = _Acc::_Prev(_Ptr);
return *this;
}
const_iterator operator--(int)
{
const_iterator tmp = *this;
--* this;
return tmp;
}
bool operator==(const const_iterator& _X) const
{
return this->_Ptr == _X._Ptr;
}
bool operator!=(const const_iterator& _X) const
{
return !(*this == _X);
}
_Nodeptr _Mynode() const
{
return _Ptr;
}
};
class iterator : public const_iterator
{
typedef const_iterator base;
public:
iterator(_Nodeptr _P = NULL) :const_iterator(_P) {}
/*reference operator*() const//取得当前节点数据
{
return _Acc::_Value(_Ptr);
}
pointer operator->() const
{
return &_Acc::_Value(_Ptr);
//return &**this;迭代器迭代的值的引用
}*/
iterator& operator++()
{
base::_Ptr = _Acc::_Next(base::_Ptr);
return *this;
}
iterator operator++(int)
{
iterator tmp = *this;
++* this;
return tmp;
}
iterator& operator--()
{
base::_Ptr = _Acc::_Prev(base::_Ptr);
return *this;
}
iterator operator--(int)
{
iterator tmp = *this;
--* this;
return tmp;
}
bool operator==(const iterator& _X) const//比较两个迭代器成员是否相等
{
return this->_Ptr == _X._Ptr;
}
bool operator !=(const iterator& _X) const
{
return !(*this == _X);
}
};
public:
iterator begin() {
return iterator(_Acc::_Next(_Head));
}
iterator end() {
return iterator(_Head);
}
const_iterator begin() const
{
return const_iterator(_Acc::_Next(_Head));
}
const_iterator end() const
{
return const_iterator(_Head);
}
public:
typedef const_iterator _It;
list() :_Head(_Buynode()), _Size(0) {}
list(size_t count, const _Ty& val) :_Head(_Buynode()), _Size(0)
{
insert(begin(), count, val);
}
list(const _Ty* _F, const _Ty* _L) :_Head(_Buynode()), _Size(0)//拷贝构造
{
insert(begin(), _F, _L);
}
list(const list& _X) :_Head(_Buynode()), _Size(0)
{
insert(begin(), _X.begin(), _X.end());
}
list& operator=(const list _X)
{
if (this == &_X)
{
return *this;
}
iterator _F1 = begin();
iterator _L1 = end();
const_iterator _F2 = _X.begin();//末尾迭代器
const_iterator _L2 = _X.end();
for (; _F1 != _L1 && _F2 != _L2; ++_F1, ++_F2)
{
*_F1 = *_F2;
}
erase(_F1, _L1);
insert(_L1, _F2, _L2);
return *this;
}
~list()
{
clear();
_Freenode(_Head);
}
void push_front(const _Ty& val)
{
insert(begin(), val);
}
void push_back(const _Ty& val)
{
insert(end(), val);
}
void insert(iterator _P, const _Ty* _F, const _Ty* _L)
{
for (; _F != _L; ++_F)
{
insert(_P, *_F);
}
}
void insert(iterator _P, size_t count, const _Ty& val)
{
while (count--)
{
insert(_P, val);
}
}
void insert(iterator _P, _It _F, _It _L)
{
for (; _F != _L; ++_F)
{
insert(_P, *_F);
}
}
iterator insert(iterator _P, const _Ty& val)//插入元素
{
_Nodeptr _S = _P._Mynode();
_Acc::_Prev(_S) = _Buynode(_Acc::_Prev(_S), _S);
_S = _Acc::_Prev(_S);
_Acc::_Next(_Acc::_Prev(_S)) = _S;
new(&_Acc::_Value(_S)) _Ty(val);
_Size += 1;
return iterator(_S);
}
void pop_front()
{
erase(begin());
}
void pop_back()
{
erase(--end());
}
/*失效迭代器
void erase(iterator _F, iterator _L)
{
for (; _F != _L; )
{
erase(_F++);
}
}
*/
void erase(iterator _F, iterator _L)
{
for (; _F != _L; )
{
erase(_F++);
}
}
void clear()
{
erase(begin(), end());
}
void remove(const Ty& val)//从前往后遍历删除一个元素
{
iterator _F = begin(), _L = end();
while (_F!=_L)
{
if (*_F == val)
{
erase(_F);
break;
}
}
}
void remove_all(const _Ty& val)//删除所有值为val的节点
{
iterator _F = begin(), _L = end();
while (_F != _L)
{
if (*_F == val)
{
erase(F++);
}
}
}
iterator erase(iterator _P)
{
_Nodeptr _S = _P++.Mynode();
//_Nodeptr _S = _P.operator++(0)._Mynode();//调动后置++,后置++是一个函数,不是一个重载的运算符
_Acc::_Next(_Acc::_Prev(_S)) = _Acc::_Next(_S);
_Acc::_Prev(_Acc::_Next(_S)) = _Acc::_Prev(_S);
(&_Acc::_Value(_S))->~_Ty;//调动析构函数
_Freenode(_S);
return _P;
}
void Swap(list& _X)
{
yhp::Swap(this->Head, _X._Head);
yhp::Swap(this->Size, _X._Size);
}
private:
_Nodeptr _Buynode(_Nodeptr _Parg = NULL, _Nodeptr _Narg = NULL)//节点空间申请
{
_Nodeptr _S = (_Nodeptr)malloc(sizeof(_Node));
_Acc::_Prev(_S) = _Parg == NULL ? _S : _Parg;
_Acc::_Next(_S) = _Narg == NULL ? _S : _Narg;
return _S;
}
void _Freenode(_Nodeptr _P)
{
free(_P);
}
_Nodeptr _Head;//
size_type _Size;//双链表中数据节点个数
};
}
#endif
四、构建对象
1.就地构建
alist.emplace_back(12);//就地构建