STL——stack容器、queue容器、list容器

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

stack容器

stack容器——基本概念

就是栈
栈不允许有遍历行为
是先进后出的数据结构
STL——stack容器、queue容器、list容器

stack容器——常用接口

STL——stack容器、queue容器、list容器

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stack>
using namespace std;

//栈stack容器

void test01()
{
	//特点:符合先进后出数据结构
	stack<int>s;

	//入栈
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);

	cout << "栈的大小:" << s.size() << endl;
	//只有栈不为空,查看栈顶,并且执行出栈操作
	while (!s.empty())
	{
		//查看栈顶元素
		cout << "栈顶元素为:" << s.top() << endl;
		//出栈
		s.pop();
	}
	cout << "栈的大小:" << s.size() << endl;
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

STL——stack容器、queue容器、list容器

queue容器

queue容器——基本概念

是先进先出的数据结构
STL——stack容器、queue容器、list容器
**队列容器允许从一端新增元素,从另一端移除元素
队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为
队列中进数据称为—入队push
队列中出数据称为—出队pop
**
STL——stack容器、queue容器、list容器

queue容器——常用接口

STL——stack容器、queue容器、list容器

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <string>
using namespace std;

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

//队列 Queue
void test01()
{
	//创建队列
	queue<Person>q;

	//准备数据
	Person p1("唐僧", 30);
	Person p2("孙悟空", 30);
	Person p3("猪八戒", 30);
	Person p4("沙僧", 30);

	//入队
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);

	cout << "队列的大小为:" << q.size() << endl;

	//判断只要队列不为空,查看对头,查看队尾,出队
	while (!q.empty())
	{
		//查看对头元素
		cout << "对头元素 --- 姓名:" << q.front().m_Name << " 对头元素 --- 年龄:" << q.front().m_Age << endl;
		//查看对尾元素 
		cout << "对尾元素 --- 姓名:" << q.back().m_Name << " 对尾元素 --- 年龄:" << q.back().m_Age << endl;
		q.pop();
	}
	cout << "队列的大小为:" << q.size() << endl;
}
int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

STL——stack容器、queue容器、list容器

list容器

list容器基本概念

STL——stack容器、queue容器、list容器
在STL中这个链表是双向链表
STL——stack容器、queue容器、list容器
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
list的优点∶
采用动态存储分配,不会造成内存浪费和溢出
链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
list的缺点:
链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大
List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。

list和vector是经常被使用的容器

list容器——构造函数

STL——stack容器、queue容器、list容器


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

void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//加上const防止修改代码
		//容器中的数据不可以修改了
		//*it = 100; err
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//创建list容器
	list<int>L1;//默认构造
	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//遍历容器
	printList(L1);

	//区间方式构造
	list<int>L2(L1.begin(), L1.end());
	printList(L2);

	//拷贝构造
	list<int>L3(10, 1000);
	printList(L3);

	//n个elem
	list<int>L4(L3);
	printList(L4);
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

STL——stack容器、queue容器、list容器

list容器——赋值和交换

STL——stack容器、queue容器、list容器

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

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L1;
	
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);

	list<int>L2;
	L2 = L1;
	printList(L2);

	list<int>L3;
	L3.assign(L2.begin(), L2.end());
	printList(L3);

	list<int>L4;
	L4.assign(10, 100);
	printList(L4);
}

//交换
void test02()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	list<int>L2;
	L2.assign(10, 100);
	cout << "交换前: " << endl;
	printList(L1);
	printList(L2);

	L1.swap(L2);
	cout << "交换后:" << endl;
	printList(L1);
	printList(L2);
}

int main()
{
	//test01();
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

STL——stack容器、queue容器、list容器

List容器——大小操作

STL——stack容器、queue容器、list容器

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

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);

	//判断容器是否为空
	if (L1.empty())
	{
		cout << "L1为空" << endl;
	}
	else
	{
		cout << "L1不为空" << endl;
		cout << "L1的元素个数为:" << L1.size() << endl;
	}

	//重新指定大小
	L1.resize(10,10000);
	printList(L1);

	L1.resize(2);
	printList(L1);
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

STL——stack容器、queue容器、list容器

list容器——插入和删除

STL——stack容器、queue容器、list容器

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

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L;
	
	//尾插
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);

	//头插
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);
	
	printList(L);

	//尾删
	L.pop_back();
	printList(L);

	//头删
	L.pop_front();
	printList(L);

	//insert插入
	list<int>::iterator it = L.begin();
	L.insert(++it, 1000);
	printList(L);

	//list容器是双向迭代器  不能跳跃移动
	//L.insert(L.begin() + 1, 100000);//err
	//printList(L);

	//删除
	it = L.begin();
	L.erase(++it);
	printList(L);

	//移除
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	printList(L);
	L.remove(10000);//按值删除  删除所有的10000
	printList(L);

	//清空
	L.clear();
	printList(L);
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}


STL——stack容器、queue容器、list容器

list容器——数据存取

list容器底层是链表 存储的不是连续的空间 不支持随机访问 不能用[]或者at直接进行访问

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

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//L1[0]不可以用 []访问list容器中的元素
	//L1[0];err
	//L1.at(0);err
	//原因是list本质是链表,不是用连续的线性空间存储的,迭代器也是不支持随机访问的

	cout << "第一个元素为: " << L1.front() << endl;
	cout << "最后一个元素为:" << L1.back() << endl;

	//验证迭代器是不支持随机访问的
	list<int>::iterator it = L1.begin();
	it++;//right 支持双向++  --
	it--;//right
	//it = it + 1;//err
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

STL——stack容器、queue容器、list容器

list容器——反转和排序

STL——stack容器、queue容器、list容器

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

//list容器赋值和交换
void printList(const list<int>&d)
{
	for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	list<int>L1;

	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(40);
	L1.push_back(30);

	cout << "反转前:" << endl;
	printList(L1);

	//反转
	L1.reverse();
	cout << "反转后:" << endl;
	printList(L1);
}

//如果返回为true,则让两个参数进行交换,如果返回false则不动
bool myCompare(int v1, int v2)
{
	//降序  就让第一个数 > 第二个数
	return v1 > v2;
}

//排序
void test02()
{
	list<int>L1;

	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(40);
	L1.push_back(30);
	
	//排序
	cout << "排序前: " << endl;
	printList(L1);

	//所有不支持随机访问迭代器的容器,不可以用标准的算法
	//不支持随机访问迭代器的容器,内部会提供对应的一些算法
	//sort(L1.begin(),L1.end());//err
	L1.sort();//默认排序规则 从小到大 升序
	cout << "排序后: " << endl;
	printList(L1);

	L1.sort(myCompare);
	printList(L1);
}

int main()
{
	//test01();
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

STL——stack容器、queue容器、list容器

list容器——排序案例

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高排序
规则:按照年龄进行升序,如果年龄相同按照身高进行降序

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <string>
using namespace std;

class Person
{
public:
	Person(string name,int age,int height)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}

	string m_Name;
	int m_Age;
	int m_Height;
};

//指定排序规则
bool comparePerson(Person &p1,Person &p2)
{
	if (p1.m_Age == p2.m_Age)
	{
		//年龄相同  按照身高降序
		return p1.m_Height > p2.m_Height;
	}
	else
	{
		//按照年龄  升序
		return p1.m_Age < p2.m_Age;
	}
}

void test01()
{
	list<Person>L1;

	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("关羽", 35, 160);
	Person p6("张飞", 35, 200);

	L1.push_back(p1);
	L1.push_back(p2);
	L1.push_back(p3);
	L1.push_back(p4);
	L1.push_back(p5);
	L1.push_back(p6);

	for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高: " << (*it).m_Height << endl;
	}

	//排序
	cout << "-----------------------------" << endl;
	cout << "排序后:" << endl;

	L1.sort(comparePerson);
	for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高: " << (*it).m_Height << endl;
	}
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

STL——stack容器、queue容器、list容器文章来源地址https://www.toymoban.com/news/detail-488728.html

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

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

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

相关文章

  • 【STL】stack、queue基本使用和模拟实现

    目录 前言 stack 接口介绍 模拟实现 queue 接口介绍 模拟实现 没有迭代器  deque介绍 stack 和 queue 本质上是一种容器配接器,就像我们平时充电时使用的电源适配器,能够将电压转换成设备能够接受的程度。 其通过封装特定容器作为其底层容器的类,通过一组特定的成员函数来

    2024年02月07日
    浏览(24)
  • STL容器适配器 -- stack和queue(使用+实现)(C++)

    栈和队列数据结构+画图分析如果对栈和队列的结构不了解的,可以先看该链接的内容 使用stack时需要头文件 #includestack stack是一种容器适配器,用于具有 后进先出 (LIFO)的环境中。只能从容器的一端(栈顶),执行删除、插入和提取操作。 stack是作为容器适配器实现的,容器

    2024年02月14日
    浏览(48)
  • 【STL】容器适配器stack和queue常见用法及模拟实现

    1.stack介绍及使用 1.1 stack的介绍 stack文档介绍 stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行元素的插入与提取操作。 stack是作为容器适配器被实现的,容器适配器是使用特定容器类的封装对象作为其基础容器的类,提供一

    2024年02月06日
    浏览(36)
  • 【C++】STL中stack,queue容器适配器的模拟实现(使用deque容器)

    🌏博客主页: 主页 🔖系列专栏: C++ ❤️感谢大家点赞👍收藏⭐评论✍️ 😍期待与大家一起进步! 虽然stack和queue中也可以存放元素,但在STL中并没有将其划分在容器的行列,而是将其称为容器适配器,这是因为stack和队列只是对其他容器的接口进行了包装,STL中stack和

    2024年02月15日
    浏览(35)
  • 【C++】STL之容器适配器——使用deque适配stack和queue

    个人主页:🍝在肯德基吃麻辣烫 分享一句喜欢的话:热烈的火焰,冰封在最沉默的火山深处。 本文章主要介绍容器适配器的功能,以及一个适配的场景。 容器适配器,按字面意思理解的话,就是用来对一个容器进行匹配的。在C++STL中,容器有:vector,list,deque,map,set等。

    2024年02月16日
    浏览(43)
  • 【C++】STL中的容器适配器 stack queue 和 priority_queue 的模拟实现

    适配器是一种设计模式 (设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口。 例如我们常见的充电器就是一种适配器,它将我们常用的220V交流电压转化为4,5V (或者其他更高的电

    2023年04月26日
    浏览(48)
  • 容器适配器---deque和STL ---stack queue priority_queue的模拟实现 C++

    目录 一、容器适配器 deque原理 deque的缺陷 deque的优势 二、stack的模拟实现  三、queue的模拟实现 四、优先级队列的模拟实现 适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户

    2024年02月02日
    浏览(43)
  • 【C++入门到精通】C++入门 —— 容器适配器、stack和queue(STL)

    文章绑定了VS平台下std::stack和std::queue的源码,大家可以下载了解一下😍 前面我们讲了C语言的基础知识,也了解了一些数据结构,并且讲了有关C++的命名空间的一些知识点以及关于C++的缺省参数、函数重载,引用 和 内联函数也认识了什么是类和对象以及怎么去new一个 ‘对象

    2024年02月12日
    浏览(34)
  • STL——stack和queue

    stl中提供了栈和队列配接器供我们使用,以后就可以直接使用了。不需要我们自己造轮子。 使用细节参考文档就可以,与之学过的容器并无二致。栈和队列的特性我们再学习数据结构时已经了解了。这里就不在赘述了。 stack - C++ Reference (cplusplus.com) queue - C++ Reference (cplusplus

    2024年02月12日
    浏览(26)
  • C++ STL stack & queue

    目录 一.stack 介绍  二.stack 使用 三.stack 模拟实现 普通版本: 适配器版本: 四.queue的介绍 五. queue使用 六.queue模拟实现 七.deque介绍 1.容器适配器 2.deque的简单介绍 3.deque的缺陷 4.为什么选择deque作为stack和queue的底层默认容器 stack------reference 1. stack是一种容器适配器,专门用在

    2024年02月12日
    浏览(26)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包