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);
	}
}
相关推荐
码匠许师傅23 分钟前
【C++ 面试真题】聊聊 volatile 和 mutable
开发语言·c++·面试
(Charon)32 分钟前
【C++】线程安全队列(二):生产者消费者模型与 condition_variable 阻塞等待
c语言·开发语言·c++
liwulin050643 分钟前
【PYTHON】使用Selenium + ChromeDriver以及XPATH语法
开发语言·python·selenium
JavaPub-rodert1 小时前
我又把自己的 Go 后台管理系统升级了一遍:文件管理、2GB 上传、私有文件预览、Docker 镜像全安排上了
开发语言·docker·golang·shiyuadmin
动力 continue1 小时前
Python 元类与异常类:类的“制造工厂”与“报错定制师”
开发语言·python·制造
quantdash_cc1 小时前
基于 QuantDash 5 分钟 K 线的网格交易策略参数网格搜索寻优实战
android·开发语言·pandas·量化·quantdash
147API1 小时前
Python批量生成蒸馏数据,并发、重试、幂等和断点续跑
开发语言·jvm·python
青 春 记 忆1 小时前
零基础入门python04:用注册校验理解字典、集合和条件判断
开发语言·vscode·python·python3.11
kels88991 小时前
实战排坑:黄金实时API开发,XAUUSD Tick报文异常处理实践
开发语言·python·websocket·网络协议·信息可视化
阿pin1 小时前
Java随笔-JDK7 HashMap头插法为何能导致死循环?
java·开发语言·hashmap