[STL]list使用介绍

这篇具有很好参考价值的文章主要介绍了[STL]list使用介绍。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

[STL]list使用

注:本文测试环境是visual studio2019。

1. list介绍

  1. list是可以在常量时间内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
  2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
  4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置。

2. 构造函数

[STL]list使用介绍,C++,c++,list,开发语言

(1)构造函数

默认构造函数。

list<int> l; //创建一个空list

(2)构造函数

创建一个有n个结点,结点数据为val的list。

list<int> l(10, 66); 

(3)构造函数

使用迭代器构造列表。

vector<int> v(10, 6);
list<int> l(v.begin(), v.end()); //创建一个存储int类型的链表,使用v初始化数据

(4)构造函数

拷贝构造函数

list<int> l1(10,6);
list<int> l2(l1); //使用l2拷贝构造l1

3. 迭代器相关函数

begin函数和end函数

begin函数:

返回指向list第一个结点的正向迭代器。

end函数:

返回指向list结尾的正向迭代器。

#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl; //输出为 1 2 3 4
	return 0;
}

rbegin函数和rend函数

rbegin函数:

返回指向list最后一个含有数据的结点的反向迭代器。

rend函数:

指向list反向结尾的反向迭代器。

#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	list<int>::reverse_iterator it = l.rbegin();
	while (it != l.rend())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl; //输出为 4 3 2 1
	return 0;
}

4. 容量相关函数

empty函数

判断list是否为空.

#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	list<int> l1;
	list<int> l2(5, 6);
	cout << l1.empty() << endl;//输出为1
    cout << l2.empty() << endl;//输出为0
	return 0;
}

size函数

获取list存储的结点个数。

#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	cout << l.size() << endl;  //输出为4
	return 0;
}

5. 数据修改函数

push_back函数和pop_back函数

push_back函数:

在list结尾插入结点。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	cout << l.size() << endl; //输出为3
	return 0;
}

pop_back函数:

在list结尾删除结点。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	cout << l.size() << endl; //输出为3
	l.pop_back();
	l.pop_back();
	cout << l.size() << endl; //输出为1
	l.pop_back();
	//l.pop_back(); -- 报错 -- 没有结点可删
	return 0;
}

push_front函数和pop_front函数

push_front函数:

在list中头插结点。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	l.push_front(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 4 3 2 1
	return 0;
}

pop_front函数:

在list中头删结点。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	l.push_front(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	l.pop_front();
	l.pop_front();
	it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
    cout << endl; // 输出为 2 1
	return 0;
}

insert函数和erase函数

和vector容器类似,list容器也没有提供find函数,而insert函数和erase函数是需要配合迭代器使用的,因此需要使用算法库的find函数。

[STL]list使用介绍,C++,c++,list,开发语言

find函数查找成功会返回指向数据的迭代器,失败会返回传入的last迭代器,注意find函数的查找范围是从first迭代器至last迭代器前,不包括last迭代器。

insert函数

功能1: 在某一位置插入结点。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.insert(pos, 66);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; //输出为1 66 2 3 4
	return 0;
}

功能2: 在某一位置插入n个存储相同数据的结点。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.insert(pos, 3, 66);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; //输出为1 66 66 66 2 3 4
	return 0;
}

功能3: 传入其他list容器或者其他类型容器的迭代器,将传入的迭代器区间内的数据插入。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l1(3, 66);
	list<int> l2;
	l2.push_back(1);
	l2.push_back(2);
	l2.push_back(3);
	list<int>::iterator pos = find(l2.begin(), l2.end(), 2);
	l2.insert(pos, l1.begin(), l1.end());
	list<int>::iterator it = l2.begin();
	while (it != l2.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;  //输出为1 66 66 66 2 3
	return 0;
}

erase函数

功能1: 删除迭代器指向的结点。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.erase(pos);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 1 3
	return 0;
}

功能2: 将迭代器范围的结点都删除。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	list<int>::iterator start = find(l.begin(), l.end(), 2);
	list<int>::iterator finish = find(l.begin(), l.end(), 5);
	l.erase(start, finish);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 1 5
	return 0;
}

swap函数

将两个list数据交换,通过交换list指向的头结点实现。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1(3, 66);
	list<int> l2;
	l2.push_back(1);
	l2.push_back(2);
	l2.push_back(3);
	l1.swap(l2);
	list<int>::iterator it1 = l1.begin();
	while (it1 != l1.end())
	{
		cout << *it1 << " ";  //输出为 1 2 3
		++it1;
	}
	cout << endl;
	list<int>::iterator it2 = l2.begin();
	while (it2 != l2.end())
	{
		cout << *it2 << " "; //输出为 66 66 66
		++it2;
	}
	cout << endl;
	return 0;
}

resize函数

[STL]list使用介绍,C++,c++,list,开发语言

  1. 如果n < size, 会将结点删除至list只有n个结点,由于list结点的释放成本是不高的,因此不同于vector只是将末尾指向修改,list会实际上的在调用resize函数时的删除结点。
  2. 如果n > size,就将插入结点使得list有n个结点
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	cout << l.size() << endl; //输出为5
	l.resize(3); // n < size
	cout << l.size() << endl; //输出为3
	l.resize(6); // n > size
	cout << l.size() << endl; //输出为6
	return 0;
}

clear函数

释放所有结点,使得list为空链表。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l(66, 6);
	cout << l.size() << endl; // 输出为66
	l.clear();
	cout << l.size() << endl; // 输出为0
	return 0;
}

6. 数据操作函数

sort函数

对list中的数据进行排序。

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

int main()
{
	list<int> l1;
	l1.push_back(2);
	l1.push_back(1);
	l1.push_back(4);
	l1.push_back(7);
	l1.push_back(5);
	l1.push_back(9);
	l1.push_back(8);

	l1.sort();
	list<int>::iterator it = l1.begin();
	it = l1.begin();
	while (it != l1.end())
	{
		cout << *it << ' ';  //输出为: 1 2 4 5 7 8 9
		it++;
	}

	return 0;
}

注: 由于list是链表实现的,迭代器是双向迭代器,因此不能调用algorithm库内的sort函数,因此需要单独设计sort函数,但是list的sort函数由于结构原因导致性能不高。

reverse函数

将list内的结点逆置。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' ';  //输出为: 1 2 3 4
		it++;
	}
	l.reverse(); //将list逆置
	it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' '; //输出为: 4 3 2 1
		it++;
	}
    cout << endl;
	return 0;
}

merge函数

如果两个list有序并且排序方式相同,可以将一个list的结点连接到另一个list上并保持有序,排序方式和连接前相同。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);
    
	list<int> l2;
	l2.push_back(6);
	l2.push_back(7);
	l2.push_back(8);
	l2.push_back(9);
    
	l2.merge(l1); //将l1的结点连接到l2,连接后l1为空
    
	list<int>::iterator it = l2.begin();
	while (it != l2.end())
	{
		cout << *it << ' ';
		it++;
	}
    cout << endl;
	return 0;
}

unique函数

只能给数据有序的list进行数据去重操作,如果数据无序去重操作会出现问题。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
    l.push_back(2);
    l.push_back(2);
	l.push_back(3);
	l.push_back(3);
	l.push_back(3);
	l.push_back(4);
	l.unique();	//对list内的数据去重
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' ';	// 输出为: 1 2 3 4
		it++;
	}
    cout << endl;
	return 0;
}

remove函数

查找对应的数据并进行删除操作,数据存在就删除,不存在不会进行任何操作。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);

	l.remove(3); //删除数据为3的结点
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' '; //输出为: 1 2 4
		it++;
	}
    cout << endl;
	return 0;
}

splice函数

将以一个list的指定部分的结点转移给其他list。文章来源地址https://www.toymoban.com/news/detail-616269.html

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

int main()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);

	list<int> l2;
	l2.push_back(5);
	l2.push_back(6);
	l2.push_back(9);
	l2.push_back(8);
	
	list<int>::iterator it = l1.begin();
	l1.splice(it, l2); //将l2的所有结点转移到l1的begin位置
	it = l1.begin();
	while (it != l1.end())
	{
		cout << *it << ' '; //输出为:5 6 9 8 1 2 3 4
		it++;
	}

	return 0;
}

到了这里,关于[STL]list使用介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [STL-list]介绍、与vector的对比、模拟实现的迭代器问题

     list的底层是 带头双向链表 结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。 与其他的序列式容器相比(array,vector,deque),list通常 在任意位置进行插入、移除元素的执行效率更好 list最大的缺陷是 不支持任意位

    2024年04月15日
    浏览(62)
  • 【STL】list的使用

    学习C++途中自然绕不过STL,在这个系列文章之中 我们讲了string的使用和string的模拟实现,以及vector的使用、vector的模拟实现。 感兴趣的可以翻翻看。 目录 系列文章 前言 默认成员函数 构造函数 拷贝构造 赋值重载 迭代器 容量查询 数据访问 数据修改 assign 头插头删尾插尾删

    2024年02月06日
    浏览(36)
  • C++STL----list的使用

    list是一种可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以 前后双向迭代 。 list的 底层是带头双向链表 ,带头双向链表中每个元素存储在独立结点当中,在结点中通过指针指向其前一个元素和后一个元素。 list与forward_list非常相似,最主要的不同

    2024年02月07日
    浏览(334)
  • STL好难(4):list的使用

    和列表很像 点击这里查看 list 的官方文档 list类似数据结构中的 链表 1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且 该容器可以前后双向迭代。 2. list的底层是双向链表结构 ,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指

    2024年02月13日
    浏览(29)
  • C++ [STL之list的使用]

    本文已收录至《C++语言》专栏! 作者:ARMCSKGT vector是一片连续的空间,在数据访问上性能较好,但是任意位置插入删除性能较低,头插头删性能亦是如此;此时在这种需要频繁插入的场景下,显然链表是一种更好的选择,STL中实现了带头双选循环链表,本次我们来介绍该如何

    2024年02月07日
    浏览(35)
  • C++ STL- list 的使用以及练习

    目录 0.引言 1. list 介绍  2. list 使用 2.1 构造函数 2.2 list iterator 的使用  3 list capacity  4. list element access  5. list modifiers  6. list 迭代器失效  7. list 与vector 对vector 8. OJ 题讲解  删除链表的倒数第 N  个节点: 本篇博客我们将介绍 STL 中 list 的使用,由于list STL 接口函数与之前

    2024年03月26日
    浏览(37)
  • 【C++STL基础入门】list基本使用

    STL(Standard Template Library)是C++标准库的一个重要组成部分,提供了一套丰富的数据结构和算法,可以大大简化C++程序的开发过程。其中,list容器是STL提供的一种双向链表实现的数据结构,具有高效的插入和删除操作,适用于需要频繁插入和删除元素的场景。本文将介绍list容

    2024年02月07日
    浏览(30)
  • 【STL】“list“容器从使用到模拟实现

    🎉博客主页:小智_x0___0x_ 🎉欢迎关注:👍点赞🙌收藏✍️留言 🎉系列专栏:C++初阶 🎉代码仓库:小智的代码仓库 list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。 list的底层是 双向链表结构 ,双向链表中每个元素存储在

    2024年02月16日
    浏览(36)
  • C++ STL学习之【list的使用】

    ✨个人主页: 北 海 🎉所属专栏: C++修行之路 🎊每篇一句: 图片来源 A year from now you may wish you had started today. 明年今日,你会希望此时此刻的自己已经开始行动了。 STL 中的 vector 存在头部及中部操作效率低的缺陷,需要另一种容器来弥补其短板,此时 list 就应运而生,

    2023年04月16日
    浏览(31)
  • stl_list类(使用+实现)(C++)

    list是一个可以在常熟范围内任意位置进行插入和删除的序列式容器。 底层是带头双向循环链表 (链接中有对带头双向循环链表的逻辑分析)。 (constructor)构造函数声明 接口说明 list() 无参构造 list(size_type n, const T val = T() 构造并初始化n个val list(const list x) 拷贝构造 list(InputI

    2024年02月14日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包