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

C++基础(12)——初识list

目录

1.list的简介(引用自cplusplus官网)

2.list的相关使用

2.1有关list的定义

2.1.1方式一(构造某类型的空容器)

2.1.2方式二(构造n个val的容器)

 2.1.3方式三(拷贝构造)

2.1.4方式四(用迭代器拷贝构造一段内容)

2.1.5方式五(构造数组区间)

2.2list的插入和删除

2.2.1push_front和pop_front

2.2.2push_back和pop_back

2.2.3insert

2.2.4erase

2.3迭代器的使用

2.3.1begin和end

2.3.2rbegin和rend

2.4lsit中元素的获取

2.5list大小的控制

2.5.1size

2.5.2resize

 2.5.3empty

 2.5.4clear

2.6用来操作list的函数

2.6.1sort

2.6.2splice

2.6.3remove

2.6.4remove_if

2.6.5reverse

 2.6.6assign

 2.6.7swap


1.list的简介(引用自cplusplus官网)

1.列表是序列容器,允许在序列内的任何位置进行恒定时间插入和擦除操作,以及在两个方向上的迭代。
2.列表容器实现为双向链接列表; 双向链接列表可以将它们包含的每个元素存储在不同且不相关的存储位置。通过与到它之前的元素的链接和到它之后的元素的链接的每个元素的关联,在内部保持排序。
3.它们非常类似于forward_list: 主要区别在于forward_list对象是单链表,因此它们只能向前迭代,以换取更小和更高效。
4.与其他碱基标准序列容器相比 (array,vector和deque),列表在插入,提取和移动已经获得迭代器的容器中的任何位置的元素方面通常表现更好,因此在大量使用这些元素的算法中,像排序算法。
5.list和forward_list最大的缺陷是不支持在任意位置的随机访问,其次,list还需要一些额外的空间,以保存每个结点之间的关联信息(对于存储的类型较小元素来说这可能是一个重要的因素)。

2.list的相关使用

2.1有关list的定义

2.1.1方式一(构造某类型的空容器)

list<int> lt1; //构造int类型的空容器

2.1.2方式二(构造n个val的容器)

list<int> lt2(10, 1); //构造含有10个1的int类型容器

 2.1.3方式三(拷贝构造)

list<int> lt3(lt2); //拷贝构造int类型的lt2容器

2.1.4方式四(用迭代器拷贝构造一段内容)

string s("hello world");
list<char> lt4(s.begin(),s.end()); //构造string对象某段区间的复制品

2.1.5方式五(构造数组区间)

int arr[] = { 1, 2, 3, 4, 5 };
int sz = sizeof(arr) / sizeof(int);
list<int> lt5(arr, arr + sz); //构造数组某段区间

2.2list的插入和删除

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

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

2.2.3insert

lsit中有三种插入方式:

  1. 在指定迭代器位置插入一个数。
  2. 在指定迭代器位置插入n个值为val的数。
  3. 在指定迭代器位置插入一段迭代器区间(左闭右开)。
#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);
	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;
}

注意:这里的find函数是头文件"algorithm"中的一个函数,返回的是位置的迭代器。 

2.2.4erase

list中有两钟删除方式:

1.删除迭代器位置的元素。

2.删除指定区间的元素(左闭右开)。

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

2.3迭代器的使用

2.3.1begin和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;
}

2.3.2rbegin和rend

也和之前的一样,通过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;
}

2.4lsit中元素的获取

front和back

分别用于获取list容器当中的第一个元素和获取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;
}

2.5list大小的控制

2.5.1size

用于获取当前容器当中有效元素的个数

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

2.5.2resize

存在的两种情况:(和之前一样)

  1. 当所给值大于当前的size时,将size扩大到该值,扩大的数据为第二个所给值,若未给出,则默认为容器所存储类型的默认构造函数所构造出来的值。
  2. 当所给值小于当前的size时,将size缩小到该值
#define _CRT_SECURE_NO_WARNINGS 1
#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,默认位0
	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;
}

 2.5.3empty

判空:

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

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

 2.5.4clear

清空容器:       

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

2.6用来操作list的函数

2.6.1sort

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

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

2.6.2splice

用于两个list之间的拼接,存在三种情况:

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

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

2.6.3remove

用于删除容器当中特定值的元素(可以多个删除)。    

#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.remove(3); //删除容器当中值为3的元素
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //1 4 2 2
	return 0;
}

2.6.4remove_if

用于删除容器当中满足条件的元素。

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

bool single_digit(const int& val)
{
	return val < 10;
}
int main()
{
	list<int> lt;
	lt.push_back(10);
	lt.push_back(4);
	lt.push_back(7);
	lt.push_back(18);
	lt.push_back(2);
	lt.push_back(5);
	lt.push_back(9);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //10 4 7 18 2 5 9
	lt.remove_if(single_digit); //删除容器当中值小于10的元素
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl; //10 18
	return 0;
}

2.6.5reverse

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

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

 2.6.6assign

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

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

 2.6.7swap

用于交换两个容器的内容。

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

int main()
{
	list<int> lt1(4, 2);
	list<int> lt2(4, 6);
	lt1.swap(lt2); //交换两个容器的内容
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl; //6 6 6 6
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl; //2 2 2 2
	return 0;
}

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

相关文章:

  • 【数据结构】【栈】算法汇总
  • 基于SpringBoot+Vue+MySQL的民宿预订平台
  • MySQL 实验 10:数据查询(3)—— 聚合函数与分组查询
  • S7---基本介绍
  • SpringBoot框架下的教育系统开发全解析
  • Android OpenGLES2.0开发(四):矩阵变换和相机投影
  • 软件工程-模块化
  • vue2 + View design 使用inputNumber设置默认值为undefined但展示数据为1且表单校验不通过的原因
  • 【Git原理与使用】远程操作标签管理
  • 【计算机网络】详谈TCP协议确认应答机制捎带应答机制超市重传机制连接管理机制流量管理机制滑动窗口拥塞控制延迟应答
  • [uni-app]小兔鲜-08云开发
  • 深入理解 MATLAB 中的图形对象和句柄:控制与定制可视化数据
  • 如何高效预警和定位玩家端的性能问题|UWA GPM 2.0 新品发布
  • 【机器学习】探索机器学习在医疗影像分析中的应用
  • 【计算机视觉】ch1-Introduction
  • 【Codeforces】CF 1997 E
  • 刷题 图论
  • leetcode_238:除自身以外数组的乘积
  • 【探索艺术新纪元:Midjourney中文版,让创意无界!】
  • vscode配置golang