[STL]list使用介绍

STLlist使用

注:本文测试环境是visual studio2019。

文章目录

1. list介绍

  1. list是可以在常量时间内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
  2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
  4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置。

2. 构造函数

(1)构造函数

默认构造函数。

cpp 复制代码
list<int> l; //创建一个空list

(2)构造函数

创建一个有n个结点,结点数据为val的list。

cpp 复制代码
list<int> l(10, 66); 

(3)构造函数

使用迭代器构造列表。

cpp 复制代码
vector<int> v(10, 6);
list<int> l(v.begin(), v.end()); //创建一个存储int类型的链表,使用v初始化数据

(4)构造函数

拷贝构造函数

cpp 复制代码
list<int> l1(10,6);
list<int> l2(l1); //使用l2拷贝构造l1

3. 迭代器相关函数

begin函数和end函数

begin函数:

返回指向list第一个结点的正向迭代器。

end函数:

返回指向list结尾的正向迭代器。

cpp 复制代码
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl; //输出为 1 2 3 4
	return 0;
}

rbegin函数和rend函数

rbegin函数:

返回指向list最后一个含有数据的结点的反向迭代器。

rend函数:

指向list反向结尾的反向迭代器。

cpp 复制代码
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	list<int>::reverse_iterator it = l.rbegin();
	while (it != l.rend())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl; //输出为 4 3 2 1
	return 0;
}

4. 容量相关函数

empty函数

判断list是否为空.

cpp 复制代码
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	list<int> l1;
	list<int> l2(5, 6);
	cout << l1.empty() << endl;//输出为1
    cout << l2.empty() << endl;//输出为0
	return 0;
}

size函数

获取list存储的结点个数。

cpp 复制代码
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	cout << l.size() << endl;  //输出为4
	return 0;
}

5. 数据修改函数

push_back函数和pop_back函数

push_back函数:

在list结尾插入结点。

cpp 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	cout << l.size() << endl; //输出为3
	return 0;
}

pop_back函数:

在list结尾删除结点。

cpp 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	cout << l.size() << endl; //输出为3
	l.pop_back();
	l.pop_back();
	cout << l.size() << endl; //输出为1
	l.pop_back();
	//l.pop_back(); -- 报错 -- 没有结点可删
	return 0;
}

push_front函数和pop_front函数

push_front函数:

在list中头插结点。

cpp 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	l.push_front(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 4 3 2 1
	return 0;
}

pop_front函数:

在list中头删结点。

cpp 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	l.push_front(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	l.pop_front();
	l.pop_front();
	it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
    cout << endl; // 输出为 2 1
	return 0;
}

insert函数和erase函数

和vector容器类似,list容器也没有提供find函数,而insert函数和erase函数是需要配合迭代器使用的,因此需要使用算法库的find函数。

find函数查找成功会返回指向数据的迭代器,失败会返回传入的last迭代器,注意find函数的查找范围是从first迭代器至last迭代器前,不包括last迭代器。

insert函数

功能1: 在某一位置插入结点。

cpp 复制代码
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.insert(pos, 66);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; //输出为1 66 2 3 4
	return 0;
}

功能2: 在某一位置插入n个存储相同数据的结点。

cpp 复制代码
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.insert(pos, 3, 66);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; //输出为1 66 66 66 2 3 4
	return 0;
}

功能3: 传入其他list容器或者其他类型容器的迭代器,将传入的迭代器区间内的数据插入。

cpp 复制代码
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l1(3, 66);
	list<int> l2;
	l2.push_back(1);
	l2.push_back(2);
	l2.push_back(3);
	list<int>::iterator pos = find(l2.begin(), l2.end(), 2);
	l2.insert(pos, l1.begin(), l1.end());
	list<int>::iterator it = l2.begin();
	while (it != l2.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;  //输出为1 66 66 66 2 3
	return 0;
}

erase函数

功能1: 删除迭代器指向的结点。

cpp 复制代码
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.erase(pos);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 1 3
	return 0;
}

功能2: 将迭代器范围的结点都删除。

cpp 复制代码
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	list<int>::iterator start = find(l.begin(), l.end(), 2);
	list<int>::iterator finish = find(l.begin(), l.end(), 5);
	l.erase(start, finish);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 1 5
	return 0;
}

swap函数

将两个list数据交换,通过交换list指向的头结点实现。

cpp 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1(3, 66);
	list<int> l2;
	l2.push_back(1);
	l2.push_back(2);
	l2.push_back(3);
	l1.swap(l2);
	list<int>::iterator it1 = l1.begin();
	while (it1 != l1.end())
	{
		cout << *it1 << " ";  //输出为 1 2 3
		++it1;
	}
	cout << endl;
	list<int>::iterator it2 = l2.begin();
	while (it2 != l2.end())
	{
		cout << *it2 << " "; //输出为 66 66 66
		++it2;
	}
	cout << endl;
	return 0;
}

resize函数

  1. 如果n < size, 会将结点删除至list只有n个结点,由于list结点的释放成本是不高的,因此不同于vector只是将末尾指向修改,list会实际上的在调用resize函数时的删除结点。
  2. 如果n > size,就将插入结点使得list有n个结点
cpp 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	cout << l.size() << endl; //输出为5
	l.resize(3); // n < size
	cout << l.size() << endl; //输出为3
	l.resize(6); // n > size
	cout << l.size() << endl; //输出为6
	return 0;
}

clear函数

释放所有结点,使得list为空链表。

cpp 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l(66, 6);
	cout << l.size() << endl; // 输出为66
	l.clear();
	cout << l.size() << endl; // 输出为0
	return 0;
}

6. 数据操作函数

sort函数

对list中的数据进行排序。

c++ 复制代码
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l1;
	l1.push_back(2);
	l1.push_back(1);
	l1.push_back(4);
	l1.push_back(7);
	l1.push_back(5);
	l1.push_back(9);
	l1.push_back(8);

	l1.sort();
	list<int>::iterator it = l1.begin();
	it = l1.begin();
	while (it != l1.end())
	{
		cout << *it << ' ';  //输出为: 1 2 4 5 7 8 9
		it++;
	}

	return 0;
}

注: 由于list是链表实现的,迭代器是双向迭代器,因此不能调用algorithm库内的sort函数,因此需要单独设计sort函数,但是list的sort函数由于结构原因导致性能不高。

reverse函数

将list内的结点逆置。

c++ 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' ';  //输出为: 1 2 3 4
		it++;
	}
	l.reverse(); //将list逆置
	it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' '; //输出为: 4 3 2 1
		it++;
	}
    cout << endl;
	return 0;
}

merge函数

如果两个list有序并且排序方式相同,可以将一个list的结点连接到另一个list上并保持有序,排序方式和连接前相同。

c++ 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);
    
	list<int> l2;
	l2.push_back(6);
	l2.push_back(7);
	l2.push_back(8);
	l2.push_back(9);
    
	l2.merge(l1); //将l1的结点连接到l2,连接后l1为空
    
	list<int>::iterator it = l2.begin();
	while (it != l2.end())
	{
		cout << *it << ' ';
		it++;
	}
    cout << endl;
	return 0;
}

unique函数

只能给数据有序的list进行数据去重操作,如果数据无序去重操作会出现问题。

c++ 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
    l.push_back(2);
    l.push_back(2);
	l.push_back(3);
	l.push_back(3);
	l.push_back(3);
	l.push_back(4);
	l.unique();	//对list内的数据去重
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' ';	// 输出为: 1 2 3 4
		it++;
	}
    cout << endl;
	return 0;
}

remove函数

查找对应的数据并进行删除操作,数据存在就删除,不存在不会进行任何操作。

c++ 复制代码
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);

	l.remove(3); //删除数据为3的结点
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' '; //输出为: 1 2 4
		it++;
	}
    cout << endl;
	return 0;
}

splice函数

将以一个list的指定部分的结点转移给其他list。

c++ 复制代码
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);

	list<int> l2;
	l2.push_back(5);
	l2.push_back(6);
	l2.push_back(9);
	l2.push_back(8);
	
	list<int>::iterator it = l1.begin();
	l1.splice(it, l2); //将l2的所有结点转移到l1的begin位置
	it = l1.begin();
	while (it != l1.end())
	{
		cout << *it << ' '; //输出为:5 6 9 8 1 2 3 4
		it++;
	}

	return 0;
}
相关推荐
小鱼仙官10 分钟前
Ubuntu C++ NTP校时程序
linux·c++·ubuntu
frjc14 分钟前
阿里云 ACK 环境 Arthas 在线调试指南
开发语言·python
老当益壮梁奶奶1 小时前
Linux软件编程学习笔记(二)Linux文件IO编程入门:从fopen到fgetc
linux·c语言·c++·笔记·学习
雪的季节1 小时前
C++ 高级(泛型编程与模板)(有空再研究吧)
c++
三十岁老牛再出发1 小时前
8月21日总结
c++·python
longxiaozhang61 小时前
C#继承:让代码少写一点,偷懒的好方法
开发语言·c#
weixin_440730501 小时前
python+request实现接口-参数化小结
开发语言·python
CHHH_HHH1 小时前
【Linux系统篇】深入解析Linux文件系统:从磁盘寻址到软硬链接
linux·服务器·开发语言·后端·ubuntu
yngsqq1 小时前
窗体快速取消
java·开发语言
XiaQ09192 小时前
Linux 常用命令学习笔记(用户管理篇):用户、组与权限的钥匙
linux·开发语言·笔记·学习