c++的list

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);
	}
}
相关推荐
dialect_2 小时前
KMP算法
开发语言·c++·算法
总要学点什么3 小时前
第89篇:三维特效-螺旋圆效果——让螺旋臂卷起一圈圈外扩的涟漪
开发语言·javascript·ecmascript·webgl·cesium·空间分析·1024程序员节
ShineWinsu4 小时前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup
傲世仙尊4 小时前
多态-虚表才活到运行期符号表早已消失
c++
开开心心_Every4 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
chenbingjie_c5 小时前
C++ 多态完全指南:从虚函数重写到虚函数表原理
开发语言·c++
是main不是漫5 小时前
【双指针——移动零】数组划分类题目的入门基石
c++·算法
吴卫斌5 小时前
定投ETF实战指南(四):行业/主题ETF——定投的“进攻端“
开发语言·python·股票·量化
·present·5 小时前
CSAPP(Bomb lab)
c语言·c++
王老师青少年编程5 小时前
2026年信奥赛c++普及组csp-j第二轮复赛真题
c++·真题·csp·信奥赛