STL list容器笔记

版权声明:内容若有错误,请您务必指出,感谢让我提高并给予我建议的你! 转载请注明出处 https://blog.csdn.net/qq792358814/article/details/81292071

 List笔记

  • list简介
  1. list是双向链表,比单项链表多了个指向前方的指针。与vector对比,vector特点:随机访问快(即下标运算,指哪打哪);尾添加,不用自动申请空间,速度快;不支持快速插入和删除。List特点:随机访问快,也支持下标运算;支持快速插入和删除。
  2. forward_list是向前链表,操作都是在链表头(增删)。

 

  • list定义
  1. typedef struct Node

{

    int a;

    char c;

}*_Node;

_Node a;

 

  1. list迭代不能进行加法运算,只能通过自身的++

 

  • list的大小
  1. size可以测链表的大小,resize可以重新设定链表的大小,设定完之后,链表的元素也会随之变化。
  2. list可以使用结构体的数据。    Node no = { 12,'a' };      list<Node> ls(6, no);

 

  • list输出
  1. 因为每次定义一个链表对象,都要重新定义一个结构体对象,十分麻烦,因此可以采用另外一种直接的方式。

 struct Node

{

    int a;

    char c;

 

    Node(int d, char e)

    {

         a = d;

         c = e;

    }

};直接在结构体里定义好Node,那么,因为结构体有全局变量的属性,

    Node no = { 12,'a' };//这里不能用等于号,因为已经声明过结构体了,需要改成括号

    Node no(12, 'a');     list<Node> ls(6, no);

    eg:

ls1.push_front(Node(12, 'a'));

    ls1.push_front(Node(13, 'b'));//先输出13

    ls1.push_back(Node(14,'c'));  

ls1.insert(ite, Node(15, 'd'));

    ls1.insert(ite, 3, Node(16, 'e'));

  • list删除和修改
  1. pop_back()删除最后一个元素,pop_front()删除第一个元素
  2. 删除使用erase函数,需要配合迭代器使用,erase(ite);看迭代器的数目大小,就删除链表第几个元素,或者erase(--ls.end());需要配合“--”符号使用,删除最后一个,erase(ls.begin());会删除第一个,若加上“++”则删除第二个。
  3. Remove函数,删除中间相同值的元素。但是前提是需要判断结构体是否相等,那么需要在结构体中加入bool类型的一个判断,

bool operator==(const Node& f)

    {

         if (f.a == this->a &&f.c == this->c)

         {

             return true;

         }

         else

         {

             return false;

         }

}

  1. Unique();函数可以删除链表中重复的函数,可是只会删除两个相同的并且相邻的元素的其中一个,相同的但不相邻的不会删除。

 

五、list合拼

  1. Ls.Swap(ls1)可以将两个链表的数据交换
  2. Reserve函数list的不同于其他容器,list是一个翻转函数
  3. Sort函数是排序函数,但由于比较的是结构体,因此需要在结构体内加入一个比较函数。f.a<this->a;写小于号就是从大到小排序,写大于号就是从小到大排序。

    bool operator < (const Node& f)

    {

         if (f.a < this->a )

         {

             return true;

         }

         else

         {

             return false;

         }

    }

  1. Merge是合并两个链表的数据,ls1.merge(ls);在原链表中,必须是已经排好顺序的,然后在合并的时候,也会自动排序。
  2. Find()函数是在list中寻找一个这么一个参数的元素。

代码:

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

struct Node
{
	int a;
	char c;

	Node(int d, char e)
	{
		a = d; 
		c = e;
	}

	bool operator==(const Node& f)
	{
		if (f.a == this->a &&f.c == this->c)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool operator < (const Node& f)
	{
		if (f.a > this->a )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

};
void fun(Node& d)
{
	cout << d.a << " " << d.c << endl;
}
//void listdefine()
//{
//	list<Node> ls(5);//list多用结构体
//
//	//Node no = { 12,'a' };
//	Node no(12, 'a');
//	list<Node> ls1(6, no);
//
//	list<Node> ls2(ls1);//与ls1一样
//	//迭代器
//	list<Node>::iterator ite = ls.begin();
//	ite++;
//	list<Node> ls3(ls1.begin(), ls1.end());
//
//	
//	for_each(ls3.begin(),ls3.end(),fun);
//
//}
//void size()
//{
//	//Node no = { 12,'a' };
//	Node no(12, 'a');
//	list<Node> ls(6, no);
//	for_each(ls.begin(), ls.end(), fun);
//
//	cout << ls.size() << endl;
//	ls.resize(3);
//	cout << ls.size() << endl;
//
//	for_each(ls.begin(), ls.end(), fun);
//	cout << ls.empty() << endl;//非空为0
//
//}
void output()
{
	//Node no = { 12,'a' };
	//Node no(12, 'a');
	//list<Node> ls(6, no);

	//for (list<Node>::iterator ite = ls.begin(); ite != ls.end(); ite++)
	//{
	//	cout << ite->a << " " << ite->c << endl;
	//}//输出与for_each一样
	//for_each(ls.begin(), ls.end(), fun);
	//cout << ls.back().a << " " << ls.back().c << endl;
	//cout << ls.front().a << " " << ls.front().c << endl;

	list<Node> ls1;
	//Node no1 ( 12,'a' );
	ls1.push_front(Node(12, 'a'));
	ls1.push_front(Node(13, 'b'));//先输出13
	ls1.push_back(Node(14,'c'));

	list<Node>::iterator ite = ls1.begin();
	ite++;
	ite++;//加两次 位置,不加则从头插入

	ls1.insert(ite, Node(15, 'd'));
	ls1.insert(ite, 3, Node(16, 'e'));
	
	for_each(ls1.begin(), ls1.end(), fun);

}
void deletechang()
{
	Node no(12, 'a');
	list<Node> ls1;

	ls1.push_front(Node(14, 'c'));//头增加			//3
	ls1.push_front(Node(13, 'b'));					//2
	ls1.push_front(Node(12, 'a'));                  //1

	ls1.push_back(Node(15, 'd'));//尾增加			//4
	ls1.push_back(Node(16, 'e'));					//5
	ls1.push_back(Node(17, 'f'));					//6
	//=====================================删除=============================================
	//ls1.pop_back();//尾删除
	//ls1.pop_front();//头删除
	list<Node>::iterator ite = ls1.begin();
	//ite++; ite++;//原来指向第一个,加两次指向第三个,即12
	//ls1.erase(ite);
	//ls1.erase(--ls1.end());//尾删除
	//ls1.erase(++ls1.begin());//删除第二个
	//ls1.clear();//全部删除
	//ls1.remove(Node(14, 'c'));//remove需要判断结构体是否相同,在结构体中加入bool类型的operator函数
	//ls1.unique();//删除重复的函数
	//=====================================修改=============================================
	ls1.assign(5,Node(17,'f'));//重新赋值
	list<Node> ls;
	ls = ls1;//重新赋值运算符

	for_each(ls1.begin(), ls1.end(), fun);
	for_each(ls.begin(), ls.end(), fun);
}
void listdo()
{
	list<Node> ls1;
	ls1.push_front(Node(12, 'a'));                  
	ls1.push_front(Node(13, 'b'));					
	ls1.push_front(Node(14, 'c'));//头增加		

	ls1.push_back(Node(15, 'd'));//尾增加			//4
	ls1.push_back(Node(16, 'e'));					//5
	ls1.push_back(Node(17, 'f'));					//6

	list<Node> ls;
	ls.push_back(Node(1, 'a'));
	ls.push_back(Node(20, 'b'));
	ls.push_back(Node(21, 'c'));

	//ls.swap(ls1);//两个可以互相交换

	//ls1.reverse();//倒转数据
	ls1.sort();//排序

	ls1.merge(ls);//合并,排序,链表必须有序的
	//ls.merge(ls1);
	list<Node>::iterator ite = find(ls1.begin(), ls1.end(), Node(14, 'c'));
	cout << ite->a << " " << ite->c << endl;
	for_each(ls1.begin(), ls1.end(), fun);
}
int main()
{
	//listdefine();
	//size();
	//output();
	//deletechang();
	listdo();

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq792358814/article/details/81292071