欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

C++入门教程 - STL第四部分:详解 queue与list容器 - 之list容器基础

最编程 2024-07-25 21:34:15
...

概念
list 容器就是一个双向循环链表

说明

  1. list 容器是本质是链表,地址存储不是连续空间的,所以不支持随机访问,所以迭代器不可以跳跃式访问其他空间的位置,就是你不可以任意的访问其他空间的元素;
  2. list 的迭代器支++ 和 – 的操作;不支持 +1 的操作,假如支持+1 的操作,就自然而然的支持 +2 的操作了,那这样就可以随机访问了,可是list 本质是链表,很明显这是不支持的。

优点
3. 相对于vector 容器来说,list 插入数据不会造成浪费空间;在 vector 插入数据时候,会开辟一段空间,通常比原有的数据大1.5倍左右,而list容器,你用到多少数据,就开辟多少空间。
4. 相对于 vector 容器来说,list 插入和删除元素的速度比较快,因为只需要找到数据的位置,修改指针域就行,不像vector 插入删除时候要大量移动数据;

1 list 的构造函数

函数原型 功能
list<T> lst ist采用采用模板类实现,对象的默认构造形式
list(beg,end) 构造函数将[beg, end)区间中的元素拷贝给本身
list(n,elem) 构造函数将n个elem拷贝给本身
list(const list &lst) 拷贝构造函数

实例:

#include <list>

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{	//模板方式初始化
	list<int>L1;
	//插入数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);
	//将L1中的元素从[begin,end)区间拷贝到L2
	list<int>L2(L1.begin(),L1.end());
	printList(L2);
	//拷贝构造方式初始化
	list<int>L3(L2);
	printList(L3);
	// 用 10 个 1000 初始化 L4
	list<int>L4(10, 1000);
	printList(L4);
}

2 list 的赋值和交换操作

函数原型 功能
assign(beg, end) 将[beg, end)区间中的数据拷贝赋值给本身
assign(n, elem) 将n个elem拷贝赋值给本身。
list& operator=(const list &lst) 重载等号操作符
swap(lst) 将lst与本身的元素互换。

实例:

#include <list>

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//赋值和交换
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);

	//赋值
	list<int>L2;
	//重载 = 号
	L2 = L1;
	printList(L2);

	list<int>L3;
	//将 L2 的元素赋值给 L1
	L3.assign(L2.begin(), L2.end());
	printList(L3);
	//把 10 个 1000 赋值给 L4;
	list<int>L4;
	L4.assign(10, 100);
	printList(L4);

}

//交换
void test02()
{

	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	list<int>L2;
	L2.assign(10, 100);

	cout << "交换前: " << endl;
	printList(L1);
	printList(L2);

	cout << endl;
	// L2 与 L1 交换 元素
	L1.swap(L2);

	cout << "交换后: " << endl;
	printList(L1);
	printList(L2);

}

3 list 的大小操作

size(); //返回容器中元素的个数

empty(); //判断容器是否为空

resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。

​ //如果容器变短,则末尾超出容器长度的元素被删除。

resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
//如果容器变短,则末尾超出容器长度的元素被删除。

函数原型 功能
size() 返回容器中元素的个数
empty() 判断容器是否为空
resize(num) /重新指定容器的长度为num,若容器变长,填充 0;如果容器变短,则末尾超出容器长度的元素被删除
resize(num, elem) 重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

实例:

#include <list>

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//大小操作
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	if (L1.empty())
	{
		cout << "L1为空" << endl;
	}
	else
	{
		cout << "L1不为空" << endl;
		cout << "L1的大小为: " << L1.size() << endl;
	}

	//重新指定大小
	L1.resize(10);
	//输出结果: 10 20 30 40 0 0 0 0 0 0 
	printList(L1);
	//输出结果:10 20 
	L1.resize(2);
	printList(L1);
}

4 list 的插入和删除

函数原型 功能
push_back(elem) 尾插elem
pop_back() 尾删
push_front(elem) 头插elem
pop_front() 尾删
insert(pos,elem) 在迭代器 pos 位置 插入elem
insert(pos,n,elem) 在迭代器pos 位置插入 n个elem
insert(pos,beg,end) 在 迭代器 pos 的位置插入区间[beg,end)的元素
clear() 移除容器的所有数据
erase(beg,end) 删除[beg,end)区间的数据
erase(pos) 删除迭代器pos位置的元素
remove(elem) 删除容器中所有与elem值匹配的元素。
#include <list>

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//插入和删除
void test01()
{
	list<int> L;
	//尾插
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	//头插
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);
	//输出结果: 300 200 100 10 20 30 
	printList(L);

	//尾删
	L.pop_back();
	//输出结果: 300 200 100 10 20 
	printList(L);

	//头删
	L.pop_front();
	//输出结果: 200 100 10 20 
	printList(L);

	//插入
	list<int>::iterator it = L.begin();
	L.insert(++it, 1000);
	//输出结果: 300 1000 200 100 10 20 
	printList(L);

	//删除
	it = L.begin();
	L.erase(++it);
	//输出结果: 300  200 100 10 20 
	printList(L);

	//移除
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	//输出结果: 300 200 100 10 20 10000 10000 10000
	printList(L);
	L.remove(10000);
	//输出结果: 300 200 100 10 20 
	printList(L);
    
    //清空
	L.clear();
	//输出结果: 
	printList(L);
}

5 list 的反转和排序

函数原型 功能
reverse() 反转链表
sort(); 链表排序

注意

  1. 排序算法是默认按升序排序;
  2. 自定义的数据类型,需要自己提供函数或者反函数作为 sort的参数,去指定排序规则;

实例:

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

bool myCompare(int val1 , int val2)
{
	return val1 > val2;
}

//反转和排序
void test01()
{
	list<int> L;
	L.push_back(90);
	L.push_back(30);
	L.push_back(20);
	L.push_back(70);
	printList(L);

	//反转容器的元素
	L.reverse();
	printList(L);

	//排序
	L.sort(); //默认的排序规则 从小到大
	printList(L);
	//提供一个函数名,改变排序算法的策略
	L.sort(myCompare); //指定规则,从大到小
	printList(L);
}