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

C++ —— 关于list

目录

链接

前言

1. 迭代器浅解

2. 接口

2.1 构造函数 

 2.2 push_back

2.3 emplace_back

2.4 insert

 2.5 erase 

 2.6 reverse 

2.7 sort

2.8 merge

 2.9 unique

 2.10 splice


链接

cplusplus.com/reference/list/list/?kw=listicon-default.png?t=O83Ahttps://cplusplus.com/reference/list/list/?kw=list


前言

list的结构其实就是一个 "带头双向循环链表",与string和vector相比,list不支持下标+[]的访问,因为链表是由一个节点一个节点连接而成的,空间并不是连续的


 

1. 迭代器浅解

 

不同性质的迭代器所支持的功能也不同,这是因为底层结构所决定的 

 如果我们使用不匹配的迭代器就会出错,比如sort就只能使用随机迭代器,因为其底层函数需要进行 - 的操作,如果是其它迭代器就会报错

list<int> lt(5, 1);
//错误,sort函数的底层要求使用随机迭代器类型
sort(lt.begin(), lt.end());

 


2. 接口

2.1 构造函数 

//n个val构造
list<int> lt(5, 1);

//迭代器遍历
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
	cout << *it << " ";
	++it;
}
cout << endl;

//范围for遍历
for (auto e : lt)
{
	cout << e << " ";
}
cout << endl;


 2.2 push_back

push_back只能尾插单个数据,无法直接插入(3,3)这样类型的函数

//n个val构造
list<int> lt(5, 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;
}

 

list<A> lt;
	A aa1(1, 1);
	lt.push_back(aa1);
    //匿名对象
	lt.push_back(A(2,2));
    //报错
	//lt.push_back(3, 3);

 


2.3 emplace_back

也是尾插函数,但是emplace_back可以直接插入(3,3)这样的数据

void test_list2()
{
	
	list<A> lt;
	A aa1(1, 1);
	lt.push_back(aa1);
	lt.push_back(A(2,2));
	//lt.push_back(3, 3);

	lt.emplace_back(aa1);
	lt.emplace_back(A(2,2));
	cout << endl;
	// 支持直接传构造A对象的参数emplace_back
	lt.emplace_back(3, 3);
}

2.4 insert

在指定位置之前插入数据,list中insert使用的是双向迭代器,不是随机迭代器,那么就不能向以前一样使用 

//不支持这样
lt.insert(it.begin()+3,30);
list<int> lt(5, 1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
 
lt.insert(lt.begin(), 10);//在首位前插入数据
 
//在第k个位置之前插入数据
auto it = lt.begin();
int k = 3;
while (k--)
{
	it++;
}
lt.insert(it, 30);
 
//迭代器遍历
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
	cout << *it << " ";
	++it;
}
cout << endl;
//范围for遍历
for (auto e : lt)
{
	cout << e << " ";
}
cout << endl;

 


 2.5 erase 

删除指定位置数据

 
	list<int> lt(5, 1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
 
	int x = 0;
	cin >> x;
	auto it = find(lt.begin(), lt.end(), x);
	//如果find没有找到就会返回第二个参数也就是lt.end()
	while (it != lt.end())
	{
		lt.erase(it);
	}
 
//迭代器遍历
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
	cout << *it << " ";
	++it;
}
cout << endl;
//范围for遍历
for (auto e : lt)
{
	cout << e << " ";
}
cout << endl;

 


 2.6 reverse 

逆置

list<int> lt(5, 1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
 
lt.reverse();
 
//迭代器遍历
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
	cout << *it << " ";
	++it;
}
cout << endl;
//范围for遍历
for (auto e : lt)
{
	cout << e << " ";
}
cout << endl;

 


2.7 sort

因为算法库中的sort函数不支持链表,所以链表自实现了一个sort函数来进行排序,默认是升序  

如果要降序的话可以使用仿函数来进行降序的调整:

1. lt.sort(greater<int>())         2. lt.sort(less<int>())

list<int> lt(5, 1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
 
lt.sort();
 
//迭代器遍历
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
	cout << *it << " ";
	++it;
}
cout << endl;
//范围for遍历
for (auto e : lt)
{
	cout << e << " ";
}
cout << endl;


2.8 merge

merge的作用是将两个有序链表进行合并,合并的前提是有序的

合并计算将second链表合并到first链表,那么second链表就会置空,其合并的原理就是取小的尾插到被合并链表

list<int> first;
first.push_back(1);
first.push_back(2);
first.push_back(3);
first.push_back(4);
 
list<int> second;
second.push_back(10);
second.push_back(20);
second.push_back(30);
second.push_back(40);
 
first.merge(second);
//范围for遍历
for (auto e : first)
{
	cout << e << " ";
}
cout << endl;
//范围for遍历
for (auto e : second)
{
	cout << e << " ";
}
cout << endl;

 

 


 2.9 unique

有序的数据去掉重复的数据

 

list<int> lt(5, 1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
 
//范围for遍历
for (auto e : lt)
{
	cout << e << " ";
}
cout << endl;
 
lt.unique();
 
//范围for遍历
for (auto e : lt)
{
	cout << e << " ";
}
cout << endl;


 2.10 splice

剪切

将链表指定的数据剪切到被粘贴的链表中去,被剪切链表中被剪切的数据会直接删除,也可以对自身进行操作,即变化自身链表数据的顺序

void test_list6()
{
	// 一个链表节点转移给另一个链表
	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
								  // mylist2 (empty)
								  // "it" still points to 2 (the 5th element


	// 调整当前链表节点的顺序
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	int x = 0;
	cin >> x;
	it = find(lt.begin(), lt.end(), x);
	if (it != lt.end())
	{
		//lt.splice(lt.begin(), lt, it);
		lt.splice(lt.begin(), lt, it, lt.end());
	}

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
}

 


                                                                        感谢观看~                                                                  


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

相关文章:

  • 数据结构:队列及其应用
  • 数组基础(c++)
  • 爬虫技术深潜:探究 JsonPath 与 XPath 的语法海洋与实战岛屿
  • VOC2007 的ImageSets/Main目录下通常有四个文件test.txt val.txt train.txt trainval.txt
  • Linux部署RabbitMQ
  • Momenta中间件C++一面-面经总结
  • Java集合(下)
  • 这条挣钱的路,离我好遥远啊
  • CSS05-复合选择器
  • 鞋类分类系统源码分享
  • 胡超:引领中美文化、创意与能源合作的关键人物
  • 【计算机网络 - 基础问题】每日 3 题(二十五)
  • MurmurHash 算法
  • 众数问题,
  • JAVA并发编程系列(11)线程池底层原理架构剖析
  • 【春秋云境】CVE-2024-23897-Jenkins 2.441之前版本存在任意文件读取漏洞
  • RuoYi若依框架学习:多环境配置
  • 电子秤PCBA方案应用解决方案设计
  • Java面试:ArrayList 和 LinkedList 的区别是什么?谈谈你对ArrayList和LinkedList的理解
  • 500. 键盘行 哈希表 力扣 Python 难度指数:3
  • JavaScript 定时器与动画基础
  • DAY80服务攻防-中间件安全HW2023-WPS 分析WeblogicJettyJenkinsCVE
  • 旧衣回收小程序搭建,开发功能优势
  • CORDIC算法笔记整理
  • 全局中断总开关位与各个中断源对应的寄存器使能位开启顺序
  • Vscode把全部‘def‘都收起来的快捷键
  • Django 对数据库的增删改查
  • [译] K8s和云原生
  • `torch.utils.data`模块
  • PostgreSQL 向量扩展插件pgvector安装和使用