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);
	}
}
相关推荐
xieliyu.5 小时前
Java算法精讲:双指针(二)
java·开发语言·算法
苏宸啊5 小时前
IPC管道
linux·c++
何以解忧,唯有..6 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
BestOrNothing_20156 小时前
ROS2 话题通信实战:消息对象、Publisher 发布器与 Subscriber 订阅器保姆级教程
c++·ros2·subscriber·publisher·话题通信
雪的季节6 小时前
RabbitMQ详解
开发语言
ice8130331817 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
三品吉他手会点灯7 小时前
C语言学习笔记 - 44.运算符和表达式 - 运算符2 - 除法与取余运算符
c语言·开发语言·笔记·算法
kkeeper~7 小时前
0基础C语言积跬步之动态内存管理
c语言·开发语言
橘右今7 小时前
2026 Java后端高频面试宝典
java·开发语言·面试
艾iYYY7 小时前
string 类的模拟实现
android·服务器·c语言·c++·算法