当前位置: 首页 > article >正文

list(一)

list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。

支持++ -- 但是不支持+和-

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

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

链表的主流访问方式就是迭代器

list构造  

1) 空容器构造函数(默认构造函数)

构造一个没有元素的空容器。

(2) 填充构造函数

构造一个包含 n 个元素的容器。每个元素都是 val 的副本。

(3) 范围构造函数

构造一个容器,其中包含与范围 [first,last] 一样多的元素,每个元素都按相同的顺序从该区域中的相应元素构造。

(4) 复制构造函数

以相同的顺序构造一个容器,其中包含 x 中每个元素的副本。

int main()
{
	
	std::list<int> first;                   //空构造           
	std::list<int> second(4, 100);                  //4个值为100的构造   
	std::list<int> third(second.begin(), second.end()); //范围构造
	std::list<int> fourth(third);                      //拷贝构造

	
	int myints[] = { 16,2,77,29 };
	std::list<int> fifth(myints, myints + sizeof(myints) / sizeof(int));//也可以使用其他容器进行范围构造

	std::cout << "The contents of fifth are: ";
	for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
		std::cout << *it << ' ';

	std::cout <<endl;

	return 0;
}
void list1()
{
	list<int>l1 = { 1,2,3,4,5,6 };
	list<int>::iterator it = l1.begin();
	while(it != l1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

}

list迭代器

【注意】
1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动

list的begin()指向头结点的下一个结点 也就是第一个有效元素的结点  end()指向头节点 也就是最后有效元素的下一个结点   rbegin()指向头结点   rend()指向头节点的下一个结点

list的成员函数empty是检测链表是否为空 并且返回布尔值  若为空 返回1 若不为空 返回0

返回链表容器中的元素数

front 和back 分别返回链表的第一个有效元素 和最后一个有效元素  有const和非const两个版本

list::push_front (const vallue_type & val)   list ::push_back(const value_type& val)

分别对链表进行头插 和尾插

list::pop_front ()   list ::pop_back()

分别对链表进行头删和尾删

(1)通过在指定位置插入单个元素

  (2)   通过在指定的位置插入 n个相同的val元素

(3)通过在指定的位置 范围插入一段内容

(1)在指定位置删除单个元素

(2)在指定的范围内删除所有元素

迭代器失效即迭代器所指向的节点的无效,即该节
点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代
器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

list无法使用alogrithm库 中的sort进行排序 

void list1()
{
	list<int>l1 = { 1,2,3,4,5,6 };
	list<int>::iterator it = l1.begin();
	while(it != l1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	sort(l1.begin(),l1.end());
}

原因是sort底层是快排 要使用随机迭代器 但是list是双向迭代器

那么链表如何排序呢

在链表的成员函数中存在专门为链表排序的sort成员函数

void list1()
{
	list<int>l1 = { 1,2,3,4,5,6 ,1,3,4};
	list<int>::iterator it = l1.begin();
	while(it != l1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	//sort(l1.begin(),l1.end());
	l1.sort();
	list<int>::iterator it1 = l1.begin();

	while (it1 != l1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
}

这是默认升序 如果想要降序那么就要传仿函数作为参数进入

void list1()
{
	list<int>l1 = { 1,2,3,4,5,6 ,1,3,4};
	list<int>::iterator it = l1.begin();
	while(it != l1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	//sort(l1.begin(),l1.end());
	l1.sort();
	list<int>::iterator it1 = l1.begin();

	while (it1 != l1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
	//greater<int>gt;
	l1.sort(greater<int>());
	for (auto e:l1)
	{
		cout << e << " ";
	}
	cout << endl;
}

在现在的链表中是存在重复的数据的 如果想要去重该如何

有专门的去重成员函数unique

void list1()
{
	list<int>l1 = { 1,2,3,4,5,6 ,1,3,4};
	list<int>::iterator it = l1.begin();
	while(it != l1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	//sort(l1.begin(),l1.end());
	l1.sort();
	list<int>::iterator it1 = l1.begin();

	while (it1 != l1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
	//greater<int>gt;
	l1.sort(greater<int>());
	for (auto e:l1)
	{
		cout << e << " ";
	}
	cout << endl;
	l1.unique();
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
}

但是list.sort排序的效率相较于快速排序的sort的效率还是非常低的 这点需要注意

拼接(转移)

接合

第一个版本 (1) 将 x 的所有元素传输到容器中。
第二个版本 (2) 仅将 i 指向的元素从 x 传输到容器中。
第三个版本 (3) 将范围 [first,last] 从 x 传输到容器中

void list2()
{

	std::list<int> mylist1, mylist2;
	std::list<int>::iterator it;

	// set some initial values:
	for (int i = 1; i <= 4; ++i)
		mylist1.push_back(i);      // mylist1: 1 2 3 4

	for (int i = 1; i <= 3; ++i)
		mylist2.push_back(i * 10);   // mylist2: 10 20 30

	it = mylist1.begin();
	++it;                         // points to 2

	mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
	for (auto e:mylist1)
	{
		cout << e << " ";
	}
	cout << endl;
}

这里使用的是第一个版本 将lsit容器x的全部元素插入 到 mylist1的pos位置  且此时链表二中被转移的内容是不会在存在在链表二的 相当于是转移

链表中不存在find 所以想要使用find需要使用算法库中的find

第一个参数和第二个参数确定查找的范围 第三个元素是要查找的元素

void list3()
{
	list<int> mylist1;
	for (int i = 1; i <= 4; i++)
		mylist1.push_back(i);
	auto it = find(mylist1.begin(),mylist1.end(),3);
	mylist1.splice(mylist1.begin() , mylist1,it);
	for (auto e : mylist1)
	{
		cout << e << " ";
	}
	cout << endl;
}


http://www.kler.cn/news/315655.html

相关文章:

  • 基于微信小程序的健身房管理系统
  • ROS第五梯:ROS+VSCode+C++单步调试
  • [Golang] Context
  • GNU链接器(LD):设置入口点(ENTRY命令)的用法及实例解析
  • 科研绘图系列:R语言箱线图(boxplot)
  • error -- unsupported GNU version gcc later than 10 are not supported;(gcc、g++)
  • 计算机毕业设计 基于SpringBoot的小区运动中心预约管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 【python】深度优先搜索文件夹并移动全部doc文件
  • 自闭症儿童寄宿学校:打造良好的学习和生活环境
  • 速盾:高防cdn除了快还有什么好处?
  • Maven国内镜像(四种)
  • 20240923 每日AI必读资讯
  • vue源码分析(九)—— 合并配置
  • ChromaDB教程_2024最新版(上)
  • 【sgCreateCallAPIFunction】自定义小工具:敏捷开发→调用接口方法代码生成工具
  • Java String trim()方法
  • 如何创建模板提示prompt
  • Linux移植之系统烧写
  • Redis 中 String 字符串类型详解
  • 2024年中国研究生数学建模竞赛B题(华为题目)WLAN组网中网络吞吐量建模一
  • libtorch落地AI项目的一些总结
  • 面试题(八)
  • OpenCV_图像膨胀腐蚀与形态学操作及具体应用详解
  • 分布式安装LNMP
  • [OpenCV] 数字图像处理 C++ 学习——16直方图均衡化、直方图比较 详细讲解+附完整代码
  • 超详细的XML介绍【附带dom4j操作XML】
  • 口腔检测系统源码分享
  • php怎么连接使用kafka
  • 【AI算法岗面试八股面经【超全整理】——NLP】
  • 学生管理系统1.0版本