STL 之 list接口的简单使用【C++】


![在这里插入图片描述](https://img-blog.csdnimg.cn/717807397d8d499d840aae2e3127f467.png

在这里插入图片描述

将文档翻译提炼得到以下结论

  1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

  2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。

  3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。

  4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。

  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

push_front &&pop_front

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

int main()
{
    
    
	list<int> lt;
	lt.push_front(0);
	lt.push_front(1);
	lt.push_front(2);
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //2 1 0
	lt.pop_front();
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //1 0
	return 0;
}

push_back&&pop_back

#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);
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //0 1 2 3
	lt.pop_back();
	lt.pop_back();
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl;//0 1
	return 0;
}

insert

1、在指定迭代器位置插入一个数。
2、在指定迭代器位置插入n个值为val的数。
3、在指定迭代器位置插入一段迭代器区间(左闭右开)。

insert以后迭代器不会失效

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

int main()
{
    
    
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	list<int>::iterator pos = find(lt.begin(), lt.end(), 2);
	//auto  pos =find (lt.begin(),lt.end() ,2);
	lt.insert(pos, 9); //在2的位置插入9
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //1 9 2 3
	pos = find(lt.begin(), lt.end(), 3);
	lt.insert(pos, 2, 8); //在3的位置插入2个8
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //1 9 2 8 8 3
	vector<int> v(2, 7);
	pos = find(lt.begin(), lt.end(), 1);
	lt.insert(pos, v.begin(), v.end()); //在1的位置插入2个7
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //7 7 1 9 2 8 8 3
	return 0;
}

erase

1、删除指定迭代器位置的元素。
2、删除指定迭代器区间(左闭右开)的所有元素。

erase 以后迭代器可能会失效

#include <iostream>
#include <algorithm>
#include <vector>
#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);
	lt.push_back(5);
	list<int>::iterator pos = find(lt.begin(), lt.end(), 2);
	lt.erase(pos); //删除2
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //1 3 4 5
	pos = find(lt.begin(), lt.end(), 4);
	lt.erase(pos, lt.end()); //删除4及其之后的元素
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //1 3
	return 0;
}

迭代器

begin&& end

在这里插入图片描述
begin函数得到容器中第一个元素的正向迭代器,通过end函数可以得到容器中最后一个元素的后一个位置的正向迭代器。

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

int main()
{
    
    
	list<int> lt(10, 2);
	//正向迭代器遍历容器
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
    
    
		cout << *it << " ";
		it++;
	}
	cout << endl;
	return 0;
}

rbegin和rend

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

int main()
{
    
    
	list<int> lt(10, 2);
	//反向迭代器遍历容器
	list<int>::reverse_iterator rit = lt.rbegin();
	while (rit != lt.rend())
	{
    
    
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
	return 0;
}

front&&back

#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;
}

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

当所给值 >当前的size时,将size扩大到该值,扩大的数据为第二个所给值,若未给出,则默认为容器所存储类型的默认构造函数所构造出来的值。

当所给值<当前的size时,将size缩小到该值

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

int main()
{
    
    
	list<int> lt(5, 3);
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //3 3 3 3 3
	lt.resize(7, 6); //将size扩大为7,扩大的值为6
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //3 3 3 3 3 6 6
	lt.resize(2); //将size缩小为2
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //3 3
	return 0;
}

empty

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

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

clear

#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;
}

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;
}

splice

1、将整个容器拼接到另一个容器的指定迭代器位置。
2、将容器当中的某一个数据拼接到另一个容器的指定迭代器位置。
3、将容器指定迭代器区间的数据拼接到另一个容器的指定迭代器位置

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

int main()
{
    
    
	list<int> lt1(4, 2);
	list<int> lt2(4, 6);
	lt1.splice(lt1.begin(), lt2); //将容器lt2拼接到容器lt1的开头
	for (auto e : lt1)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //6 6 6 6 2 2 2 2 

	list<int> lt3(4, 2);
	list<int> lt4(4, 6);
	lt3.splice(lt3.begin(), lt4, lt4.begin()); //将容器lt4的第一个数据拼接到容器lt3的开头
	for (auto e : lt3)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //6 2 2 2 2 

	list<int> lt5(4, 2);
	list<int> lt6(4, 6);
	lt5.splice(lt5.begin(), lt6, lt6.begin(), lt6.end()); //将容器lt6的指定迭代器区间内的数据拼接到容器lt5的开头
	for (auto e : lt5)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //6 6 6 6 2 2 2 2
	return 0;
}

unique

unique函数用于删除容器当中连续的重复元素。

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

int main()
{
    
    
	list<int> lt;
	lt.push_back(1);
	lt.push_back(4);
	lt.push_back(3);
	lt.push_back(3);
	lt.push_back(2);
	lt.push_back(2);
	lt.push_back(3);
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //1 4 3 3 2 2 3
	lt.sort(); //将容器当中的元素排为升序
	lt.unique(); //删除容器当中连续的重复元素
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //1 2 3 4
	return 0;
}

merge

merge函数用于将一个有序list容器合并到另一个有序list容器当中,使得合并后的list容器仍然有序。

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

int main()
{
    
    
	list<int> lt1;
	lt1.push_back(3);
	lt1.push_back(8);
	lt1.push_back(1);
	list<int> lt2;
	lt2.push_back(6);
	lt2.push_back(2);
	lt2.push_back(9);
	lt2.push_back(5);
	lt1.sort(); //将容器lt1排为升序
	lt2.sort(); //将容器lt2排为升序
	//merge前提是lt1和lt2都有序
	lt1.merge(lt2); //将lt2合并到lt1当中
	for (auto e : lt1)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //1 2 3 5 6 8 9 
	return 0;
}

reverse

reverse函数用于将容器当中元素的位置进行逆置。

#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);
	lt.push_back(5);
	lt.reverse(); //将容器当中元素的位置进行逆置
	for (auto e : lt)
	{
    
    
		cout << e << " ";
	}
	cout << endl; //5 4 3 2 1 
	return 0;
}

如果你觉得这篇文章对你有帮助,不妨动动手指给点赞收藏加转发,给鄃鳕一个大大的关注,你们的每一次支持都将转化为我前进的动力!!!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_73478334/article/details/132061405