list的介绍及使用
list的介绍
- list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向
其前一个元素和后一个元素。 - list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高
效。 - 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率
更好。 - 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list
的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间 开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这 可能是一个重要的因素)
list iterator的使用
- 可暂时将迭代器理解成一个指针,该指针指向list中的某个节点。
- begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
- rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动
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())
{
其赋值
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++);
}
}
list的模拟实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
namespace sz
{
template <class T>
struct list_node
{
typedef list_node Node;
Node* _next;
Node* _prev;
T _data;
list_node(const T& x = T())
:_data(x)
, _next(nullptr)
, _prev(nullptr)
{}
};
template <class T, class Ret, class Ptr>
struct list_iterator
{
typedef list_iterator<T, Ret, Ptr> iterator;
typedef list_node<T> Node;
Node* _node;
list_iterator(Node* node)
:_node(node)
{}
bool operator!=(const iterator& it)
{
return _node != it._node;
}
bool operator==(const iterator& it)
{
return _node == it._node;
}
Ret operator*()
{
return _node->_data;
}
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;
}
Ptr operator->()
{
return &(operator*());
}
};
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);
}
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
list()
{
empty_init();
}
template <class InputIterator>
list(InputIterator first, InputIterator last)
{
empty_init();
while (first != last)
{
push_back(*first);
first++;
}
}
void swap(list<T>& x)
{
std::swap(_head, x._head);
}
list(const list<T>& lt)
{
empty_init();
list<T> tmp(lt.begin(), lt.end());
swap(tmp);
}
list<T>& operator=(list<T> lt)
{
swap(lt);
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
void push_back(const T& x)
{
Node* tail = _head->_prev;
Node* newnode = new Node(x);
_head->_prev = newnode;
newnode->_next = _head;
tail->_next = newnode;
newnode->_prev = tail;
}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator inster(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
prev->_next = newnode;
newnode->_prev = cur;
newnode->_next = prev;
prev->_prev = newnode;
return iterator(newnode);
}
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);
}
iterator pop_back()
{
erase(--end());
}
iterator pop_front()
{
erase(begin());
}
private:
Node* _head;
};
}
void TextList1()
{
sz::list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
sz::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;
it = lt.begin();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
}
void TextList2()
{
struct Pos
{
int _aa;
int _bb;
Pos(int aa = 10, int bb = 20)
:_aa(aa)
, _bb(bb)
{}
};
Pos p1;
Pos* p2 = &p1;
p2->_aa;
p2->_bb;
sz::list<Pos> lt;
lt.push_back(Pos(10, 20));
lt.push_back(Pos(30, 40));
sz::list<Pos>::iterator it = lt.begin();
while (it != lt.end())
{
cout << it->_aa << ":" << it->_bb << endl;
it++;
}
}
void Func(const sz::list<int>& l)
{
sz::list<int>::const_iterator it = l.begin();
while (it != l.end())
{
cout << *it << " ";
++it;
}
cout << endl;
it = l.begin();
while (it != l.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
void TextList3()
{
sz::list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
Func(lt);
}
void TextList4()
{
sz::list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
sz::list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
it++;
}
cout << endl;
sz::list<int> copy = lt;
it = copy.begin();
while (it != copy.end())
{
cout << *it << " ";
it++;
}
cout << endl;
sz::list<int> lt2;
lt2.push_back(10);
lt2.push_back(20);
lt2.push_back(30);
lt = lt2;
it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
int main()
{
TextList4();
return 0;
}
list反向迭代器
reverse_reverse
这里的反向迭代器不仅适用list也适用vector
#pragma once
namespace sz
{
template<class Iterator, class Ret, class Ptr>
class _reverse_iterator
{
public:
Iterator _cur;
typedef _reverse_iterator<Iterator, Ret, Ptr> RIterator;
_reverse_iterator(Iterator it)
:_cur(it)
{}
RIterator operator++()
{
--_cur;
return *this;
}
RIterator operator++(int)
{
_cur--;
return *this;
}
RIterator operator--()
{
++_cur;
return *this;
}
RIterator operator--(int)
{
_cur++;
return *this;
}
Ret operator*()
{
auto tmp = _cur;
--tmp;
return *tmp;
}
Ptr operator->()
{
--_cur;
return &(*_cur);
}
bool operator!=(const RIterator& it)
{
return _cur != it._cur;
}
};
}
list.h
#pragma once
#include<iostream>
#include<assert.h>
#include"reverse_iterator.h"
using namespace std;
namespace sz
{
template <class T>
struct list_node
{
typedef list_node Node;
Node* _next;
Node* _prev;
T _data;
list_node(const T& x = T())
:_data(x)
, _next(nullptr)
, _prev(nullptr)
{}
};
template <class T, class Ret, class Ptr>
struct list_iterator
{
typedef list_iterator<T, Ret, Ptr> iterator;
typedef list_node<T> Node;
Node* _node;
list_iterator(Node* node)
:_node(node)
{}
bool operator!=(const iterator& it)
{
return _node != it._node;
}
bool operator==(const iterator& it)
{
return _node == it._node;
}
Ret operator*()
{
return _node->_data;
}
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;
}
Ptr operator->()
{
return &(operator*());
}
};
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;
typedef _reverse_iterator<iterator, T&, T*> reverse_iterator;
typedef _reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
const_reverse_iterator rbegin() const
{
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const
{
return const_reverse_iterator(begin());
}
reverse_iterator rbegin()
{
return reverse_iterator(end());
}
reverse_iterator rend()
{
return reverse_iterator(begin());
}
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);
}
void empty_init()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
list()
{
empty_init();
}
template <class InputIterator>
list(InputIterator first, InputIterator last)
{
empty_init();
while (first != last)
{
push_back(*first);
first++;
}
}
void swap(list<T>& x)
{
std::swap(_head, x._head);
}
list(const list<T>& lt)
{
empty_init();
list<T> tmp(lt.begin(), lt.end());
swap(tmp);
}
list<T>& operator=(list<T> lt)
{
swap(lt);
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
void push_back(const T& x)
{
Node* tail = _head->_prev;
Node* newnode = new Node(x);
_head->_prev = newnode;
newnode->_next = _head;
tail->_next = newnode;
newnode->_prev = tail;
}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator inster(iterator pos, const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* newnode = new Node(x);
prev->_next = newnode;
newnode->_prev = cur;
newnode->_next = prev;
prev->_prev = newnode;
return iterator(newnode);
}
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);
}
iterator pop_back()
{
erase(--end());
}
iterator pop_front()
{
erase(begin());
}
private:
Node* _head;
};
}
|