list的了解
基本信息
list相当于带头双向循环的链表,经典的双向迭代器;bidirectional_iterator_tag;
push和pop
可以头插和尾插,头删和尾删
cpp
#include<iostream>
#include<list>
using namespace std;
int main(){
list<int> ls;
ls.push_back(1);
ls.push_front(2);
ls.push_back(1);
ls.push_front(2);
for (auto k : ls) {
cout << k << " ";
}
cout << endl;
ls.pop_back();
ls.pop_front();
for (auto k : ls) {//有迭代器可以使用for遍历
cout << k << " ";
}
return 0;
}

list不可以直接指定删除和插入; 只可以一步步执行,读前或读后;
cpp
void test4() {
list<int>ls;
ls.push_back(1);
ls.push_back(2);
ls.push_back(3);
ls.push_back(4);
ls.push_back(5);
ls.push_back(6);
auto t = ls.begin();
int k = 3;
while (k--) {
t++;
}
ls.insert(t, 99);
for (int k : ls) {
cout << k << " ";
}
}

sort
不可以使用sort算法,sort只可以使用随机的迭代器;
cpp
void test2() {
list<int> ls;
ls.push_back(1);
ls.push_front(3);
ls.push_back(2);
ls.push_front(4);
ls.sort();
for (auto k : ls) {
cout << k << " ";
}
}

可以使用reverse;
list有自己的排序;
list的emplace_back支持传A对象的构造参数;
cpp
void test3() {
class A {
public:
A(int a, int b) {
_a = a;
_b = b;
}
int _a, _b;
};
list<A>ls;
A aa(9, 9);
ls.emplace_back(A(2,2));
ls.emplace_back(1, 1);
ls.emplace_front(aa);
for (auto k : ls) {
cout << k._a << " " << k._b << endl;
}
}

merge
list支持合并操作
cpp
void test5() {
list<int>f, s;
f.push_back(1);
f.push_back(2);
f.push_back(3);
s.push_back(4);
s.push_back(5);
s.push_back(6);
f.merge(s);
for (int k : f) {
cout << k << " ";
}
}



splice
转移把一个list的内容转移给另一个list;
cpp
void test6() {
list<int>my;
my.push_back(0);
list<int>h;
h.push_back(1);
h.push_back(2);
h.push_back(3);
h.push_back(4);
h.push_back(5);
print(h);
print(my);
my.splice(my.begin(), h);//插入位置之前和转移的list,h为空;
print(my);
}

自主实现
cpp
#pragma once
namespace fish {
template<class T>
class list_node{
public:
T _data;//数据
list_node<T>* _next;//前
list_node<T>* _prev;//后
list_node(const T& data=T() )//构造
:_data(data)
, _next(nullptr)
, _prev(nullptr)
{}
};
template<class T>
class list {
typedef list_node<T> Node;//简短输入实例为T的list_node;
public:
list() {
_head = new Node;//创建头
_head->_next = _head;//定为自己
_head->_prev = _head;
}
private:
Node* _head;//多个Node
size_t _size;
};
}
cpp
void push_back(const T& x) {//插入
Node* newnode = new Node(x);
Node* tail = _head->_prev;
tail->_next = newnode;
newnode->_next = _head;
newnode->_prev = tail;
_head->_prev = newnode;
++_size;
}
size_t size() {//数量
return _size;
}
bool empty() {//
return _size == 0;
}
cpp
struct list_iterator {//迭代器结构
typedef list_node<T> Node;
typedef list_iterator<T> Self;
Node* _node;
list_iterator(Node* node)
:_node(node)
{}
T* operator->() {
return &_node->_data;
}
T& operator*() {
return _node->_data;
}
Self& operator++() {
_node = _node->_next;
return *this;
}
bool operator!=(const Self& s) {
return _node != s._node;
}
};
typedef list_iterator<T> iterator;
iterator begin() {
iterator it(_head->_next);
return it;
}
iterator end() {
return _head;
}
cpp
void test() {
list<int>ls;
ls.push_back(1);
ls.push_back(2);
for (auto k : ls) {
cout << k << " ";
}
}

cpp
void insert(iterator pos, const &T x) {
Node* cur = pos._node;
Node* prev = pos->_prev;
Node* newnode = new Node(x);
newnode->_next = cur;
newnode->_prev = prev;
cur->_prev = newnode;
prev->_next = newnode;
++_size();
}
void erase(iterator pos) {
assert(pos != end());//加上#include<cassert>
Node* prev = pos._node->_prev;
Node* next = pos._node->_next;
prev->_next = next;
next->_prev = prev;
delete pos._node;
--_size;
}
void push_front(const T&x) {
insert(begin(), x);
}
void pop_back() {
erase(--end());
}
void pop_front() {
erase(begin());
}

cpp
~list() {
clear();
delete _head;
_head = nullptr;
}
void clear() {
auto it = begin();
while (it != end()) {
it = erase(it);
}
}
cpp
void empty_init() {
_head = new Node;
_head ->_next = _head;
_head->_prev = _head;
_size = 0;
}
list(list<T>& lt) {
empty_init();
for (auto& e : lt) {
push_back(e);
}
}