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);
	}
}
相关推荐
Lyyaoo.3 小时前
【JAVA基础面经】JVM的内存模型
java·开发语言·jvm
杨凯凡4 小时前
【017】泛型与通配符:API 设计里怎么用省心
java·开发语言
2401_8734794010 小时前
如何利用IP查询定位识别电商刷单?4个关键指标+工具配置方案
开发语言·tcp/ip·php
我爱cope10 小时前
【从0开始学设计模式-10| 装饰模式】
java·开发语言·设计模式
菜鸟学Python11 小时前
Python生态在悄悄改变:FastAPI全面反超,Django和Flask还行吗?
开发语言·python·django·flask·fastapi
浪浪小洋12 小时前
c++ qt课设定制
开发语言·c++
charlie11451419112 小时前
嵌入式C++工程实践第16篇:第四次重构 —— LED模板,从通用GPIO到专用抽象
c语言·开发语言·c++·驱动开发·嵌入式硬件·重构
handler0112 小时前
Linux: 基本指令知识点(2)
linux·服务器·c语言·c++·笔记·学习
故事和你9112 小时前
洛谷-数据结构1-4-图的基本应用1
开发语言·数据结构·算法·深度优先·动态规划·图论