STL详解(五)——list的使用

list的介绍

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

list的使用

list的定义方式

//构造一个元素类型为int的空链表
list<int> lt1;
	//生成10个结点的链表,每个值为2
	list<int> lt2(10, 2);
//生成一个链表的复制品,拷贝构造
list<int> lt3(lt2);
//用某个相同值类型容器的一段迭代区间构造
string s = "hello world";
list<char> lt4(s.begin(), s.end());

list的插入和删除

push_front和pop_front

push_front函数用于头插一个数据,pop_front函数用于头删一个数据。

#include<iostream>
using namespace std;
#include<list>
int main()
{
	list<int> lt;
	lt.push_front(1);
	lt.push_front(2);
	lt.push_front(3);
	lt.push_front(4);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;	//结果4 3 2 1
	lt.pop_front();
	lt.pop_front();
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;	//结果 2 1
	return 0;
}
push_back和pop_back

push_back函数用于尾插一个数据,pop_back函数用于尾删一个数据。


#include<iostream>
using namespace std;
#include<list>
int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;	//结果1 2 3 4
	lt.pop_back();
	lt.pop_back();
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;	//结果 1 2
	return 0;
}
insert

insert支持三种插入方式

  1. 在指定迭代器位置插入一个数。
  2. 在指定迭代器位置插入n个值为val的数。
  3. 在指定迭代器位置插入一段迭代器区间(左闭右开)。
#include<iostream>
using namespace std;
#include<list>
#include<vector>

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	list<int>::iterator pos = find(lt.begin(), lt.end(), 2);//查找值为2的位置
	lt.insert(pos, 10);	//在2位置插入一个10
	pos = find(lt.begin(), lt.end(), 3);	//查找3的位置
	lt.insert(pos, 2, 7);	//在3位置插入两个7
	vector<int> v(3, 1);
	lt.insert(lt.begin(), v.begin(), v.end());	//头部插入一段迭代区间
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;	//结果 1 1 1 1 10 2 7 7 3 4
	return 0;
}

注意: find函数是算法库“algorithm”当中的一个函数,不是list的成员函数,该函数在指定迭代器区间寻找指定值的位置,并返回该位置的迭代器。

erase

list当中的erase函数支持两种删除方式:

  1. 删除指定迭代器位置的元素。
  2. 删除指定迭代器区间(左闭右开)的所有元素
#include<iostream>
using namespace std;
#include<algorithm>
#include<list>

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	list<int>::iterator pos = find(lt.begin(), lt.end(), 2);//查找2的位置
	lt.erase(pos);	//删除2
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //结果 1 3 4
	lt.erase(lt.begin(), ++lt.begin());//删除一段区间
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //结果 3 4
	return 0;
}

list的迭代器使用

begin和end

通过begin函数可以得到容器中第一个元素的正向迭代器,通过end函数可以得到容器中最后一个元素的后一个位置的正向迭代器。

#include<iostream>
using namespace std;
#include<list>

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;	//结果1 2 3 4 5
	return 0;
}
rbegin和rend

通过rbegin函数可以得到容器中最后一个元素的反向迭代器,通过rend函数可以得到容器中第一个元素的前一个位置的反向迭代器。

#include<iostream>
using namespace std;
#include<list>

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	list<int>::reverse_iterator it = lt.rbegin();
	while (it != lt.rend())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;	//结果5 4 3 2 1
	return 0;
}

list的元素获取

front和back

front函数用于获取list容器当中的第一个元素,back函数用于获取list容器当中的最后一个元素。

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt;
	lt.push_back(0);
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	cout << lt.front() << endl; //0
	cout << lt.back() << endl; //4
	return 0;
}

list的大小控制

size

size函数用于获取当前容器当中的元素个数。

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	cout << lt.size() << endl; //4
	return 0;
}

resize

resize的两种情况:

  1. 当所给值大于当前的size时,将size扩大到该值,扩大的数据为第二个所给值,若未给出,则默认为容器所存储类型的默认构造函数所构造出来的值。
  2. 当所给值小于当前的size时,将size缩小到该值。
#include<iostream>
using namespace std;
#include<list>

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(1);
	lt.push_back(1);
	lt.push_back(1);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //结果 1 1 1 1
	lt.resize(6, 2);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //结果 1 1 1 1 2 2
	lt.resize(2);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //结果 1 1 
	return 0;
}
empty

empty函数用于判断当前容器是否为空

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt;
	cout << lt.empty() << endl; //1
	return 0;
}
clear

clear函数用于清空容器,清空后容器的size为0。

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt(5, 2);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //2 2 2 2 2
	cout << lt.size() << endl; //5
	lt.clear(); //清空容器
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //(无数据)
	cout << lt.size() << endl; //0
	return 0;
}

list的操作函数

sort

sort函数可以将容器当中的数据默认排为升序。

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> lt;
	lt.push_back(4);
	lt.push_back(7);
	lt.push_back(5);
	lt.push_back(9);
	lt.push_back(6);
	lt.push_back(0);
	lt.push_back(3);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //4 7 5 9 6 0 3
	lt.sort(); //默认将容器内数据排为升序
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //0 3 4 5 6 7 9
	return 0;
}

**注意:**这个sort不是algorithm算法库里面的排序函数,这是list的成员函数,默认升序,如果想自定义排序,传自定义的比较方法,这是一个函数模板.

splice

splice函数用于两个list容器之间的拼接,其有三种拼接方式:

  1. 将整个容器转移到另一个容器的指定迭代器位置。
  2. 将容器当中的某一个数据转移到另一个容器的指定迭代器位置。
  3. 将容器指定迭代器区间的数据转移到另一个容器的指定迭代器位置。
#include<iostream>
using namespace std;
#include<list>

int main()
{
	list<int> lt1(4, 1);
	list<int> lt2(4, 6);
	lt2.splice(lt2.begin(), lt1);//将lt1整个容器转移到lt2头部
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;	//无数据,lt1全转移走了,为空容器
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;	//1 1 1 1 6 6 6 6   lt2头部插入了lt1全部
	list<int> lt3(4, 3);
	list<int> lt4(4, 9);
	lt4.splice(lt4.begin(), lt3, lt3.begin());	//将lt4的头部元素转移到lt3的头部
	for (auto e : lt3)
	{
		cout << e << " ";
	}
	cout << endl;	//3 3 3
	for (auto e : lt4)
	{
		cout << e << " ";
	}
	cout << endl;	//3 9 9 9 9 
	list<int> lt5(4, 1);
	list<int> lt6(4, 2);
	lt6.splice(lt6.begin(), lt5, lt5.begin(), --lt5.end());//将lt5的一段迭代区间转移过去
	for (auto e : lt5)
	{
		cout << e << " ";
	}
	cout << endl;	// 1
	for (auto e : lt6)
	{
		cout << e << " ";
	}
	cout << endl;	//1 1 1 2 2 2 2 
	return 0;
}

注意: 容器当中被转移到另一个容器的数据在原容器当中就不存在了。(实际上就是将链表当中的指定结点转移到了另一个容器当中)

remove

该函数用于移除容器中特地的元素值,将容器中指定值全部移除即删除掉。

#include<iostream>
using namespace std;
#include<list>
int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(3);
	lt.push_back(5);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;	//1 2 3 4 3 5
	lt.remove(3);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;	//1 2 4 5
	return 0;
}
remove_if

remove_if,显然是满足条件的特定值就移除,条件这个函数可以自己定义,传入


#include<iostream>
using namespace std;
#include<list>

bool single_digit(const int& val)
{
	return val < 10;
}

int main()
{
	list<int> lt;
	lt.push_back(4);
	lt.push_back(19);
	lt.push_back(10);
	lt.push_back(25);
	lt.push_back(9);
	lt.remove_if(single_digit);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //19 10 25
	return 0;
}
unique

unique函数用于对容器元素进行去重,重复的多个元素值,保留一个。

#include<iostream>
using namespace std;
#include<list>

int main()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(2);
	lt.push_back(1);
	for (auto x : lt)
	{
		cout << x << " ";
	}
	cout << endl; //1 2 3 2 1
	lt.sort();	//去重前必须排序
	lt.unique();
	for (auto x : lt)
	{
		cout << x << " ";
	}
	cout << endl; //1 2 3 
	return 0;
}

注意:使用unique函数进行去重前,必须排序,否则去不了重,原因是STL中unique函数是建立在链表是有序的条件下,写的unique函数,所以无序,元素相同值不连续,去不了重。

merge

merge函数用于将一个有序list容器合并到另一个有序list容器当中,使得合并后的list容器任然有序。(类似于归并排序)

#include<iostream>
using namespace std;
#include<list>

int main()
{
	//1 7 9 18
	list<int> lt;
	lt.push_back(1);
	lt.push_back(7);
	lt.push_back(9);
	lt.push_back(18);
	//2 5 10 16
	list<int> lt2;
	lt2.push_back(2);
	lt2.push_back(5);
	lt2.push_back(10);
	lt2.push_back(16);
	lt.merge(lt2);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;//1 2 5 7 9 10 16 18
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;//无数据,由于lt2全部归并到lt1中了
	return 0;
}

注意:将一段有序链表归并到了另一段有序链表中后,该有序链表当中就无元素了,全归并过去了

reverse

reverse函数用于将链表逆置

#include<iostream>
using namespace std;
#include<list>

int main()
{
	list<int> lt1;
	lt1.push_back(1);
	lt1.push_back(2);
	lt1.push_back(3);
	lt1.push_back(4);
	lt1.push_back(5);
	lt1.reverse();	//将链表进行翻转
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

注意:此reverse函数是list的成员函数,并非是算法库algorithm里面的reverse,此reverse只能对链表整个进行逆置,没有算法库的reverse对任何类型容器一段迭代区间进行逆置,功能那么强大。

assign

assign函数用于将新内容分配给容器,替换其当前内容,新内容的赋予方式有两种:

  1. 将此链表所有内容替换成n个val值的填充
  2. 将此链表替换成某个相同类型值容器的一段迭代区间
#include <iostream>
#include <string>
#include <list>
using namespace std;

int main()
{
	list<char> lt(3, 'a');
	lt.assign(3, 'b'); //将新内容分配给容器,替换其当前内容
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //b b b
	string s("hello world");
	lt.assign(s.begin(), s.end()); //将新内容分配给容器,替换其当前内容
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //h e l l o   w o r l d
	return 0;
}
swap

swap函数用于交换两个容器的内容。

#include<iostream>
using namespace std;
#include<list>

int main()
{
	list<int> lt1(4, 2);
	list<int> lt2(4, 6);
	lt1.swap(lt2);
	for (auto x : lt1)
	{
		cout << x << " ";
	}
	cout << endl;	//6 6 6 6
	for (auto x : lt2)
	{
		cout << x << " ";
	}
	cout << endl;	//4 4 4 4
	return 0;
}
  • 13
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

维生素C++

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值