STL容器篇之stack和queue

简介: STL容器篇之stack和queue

stack

1.栈的存储顺序是固定的,先进后出的存储顺序

2.栈是不存在迭代器的

运用

1.使用容器还是要使用头文件 stack

2.

常用的内置函数

push() 入栈 ->相当于尾插

pop() 出栈->相当于尾删

top() 获取头部元素

初始化

stack<类型名> 对象

#include<iostream>
#include<string>
#include<stack>  //同样的使用stack容器,要使用相应的头文件
using namespace std;
int main()
{
  stack<int> istack;
  for (int i = 0; i < 4; i++)
  {
    istack.push(i);  //入栈  相当于尾插 push_back()
  }
  while (!istack.empty())  //判断是否为空(istack中是否有数据)
  {
    cout << istack.top(); //打印栈的头部元素
    istack.pop();     //出栈相当于尾删   pop_back();
  }
  system("pause");
  return 0;
} 

案例一:十进制变为二进制

#include<iostream>
#include<stack>
using namespace std;
int main()
{
  int number = 100;
  stack<int> istack;
  while (number != 0)
  {
    istack.push(number % 2);
    number /= 2;
  }
  while (!istack.empty())  //栈的基本操作
  {
    cout << istack.top();
    istack.pop();
  }
  system("pause");
  return 0;
}

案例二:十进制转化为十六进制

char类型和int类型的转化

主要原理是根据ASCII码

int类型变成char类型,只需要加上一个’0’,便可以

例如 :1 + ‘0’ = ‘1’

相反:char类型变成一个Int类型,只需要
减去一个’0’
,便可以

例如: ‘1’ - ‘0’ = 1

下面是代码的实现

#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
  stack<char> cstack; //注意用字符型
  int number = 999;
  while (number) //number 不为0的时候,进行循环
  {
    if (number % 16 < 10)     //注意这里的两种情况的转化
    {
      cstack.push(number % 16 + '0');
    }
    else
    {
      cstack.push(number % 16 - 10 + 'A');
    }
    number /= 16;
  }
  while (!cstack.empty())
  {
    cout << cstack.top();
    cstack.pop();
  }
  system("pause");
  return 0;
}

queue

队列存储的顺序也是特定的,普通队列,先进先出,优先队列,按照优先权出队。

这有3种

1.queue

2.deque

3.priority_queue

queue(普通队列)

这基本操作跟上面stack差不多

#include<iostream>
#include<queue>
using namespace std;
int main()
{
  queue<int> iqueue;
  for (int i = 0; i < 3; i++)
  {
    iqueue.push(i);
  }
  cout << iqueue.size() << endl; //打印大小
  cout << iqueue.back() << endl;  //打印尾部的元素
  while (!iqueue.empty())
  {
    iqueue.front();  //打印头部元素
    iqueue.pop();  //出栈
  }
  //基本操作跟stack 差不多
  system("pause");
  return 0;
}

deque(双向队列)

有尾插,尾删,头插,头删

#include<iostream>
#include<deque>
#include<string> 
using namespace std;
int main()
{
  deque<string> date1;
  date1.push_back("尾插");
  date1.push_front("头插");
  //相对应的,头插,用头删
  //尾插,用尾删
  while (!date1.empty())
  {
    cout << date1.front();
    date1.pop_front();
  }
  deque<string> date2;
  date2.push_front("温柔了");
  date2.push_back("岁月");
  while (!date2.empty())
  {
    cout << date2.back();
    date2.pop_back();
  }
}

priority_queue### 用法

priority_queue(类型,容器,比较的方法);

priority_queue(类型,容器);

如果不用比较的方法(就会采用系统默认的(less<参数>))

当然比较的方法也可以自己写

less表示数字越大优先级越大,>greater表示数字越小,优先级越大。

使用

基本使用

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main()
{
  priority_queue<int, vector<int>, less<int>> date1;
  priority_queue<int, vector<int>>  date2;
  //date1和date2 是一样的,不写最后一个参数,系统默认是less<参数类型>
  srand((unsigned int)time(nullptr));
  for (int i = 0; i < 100; i++)
  {
    date1.push(rand() % 100);
  }
  while (!date1.empty())
  {
    cout << date1.top() << endl;;
    date1.pop();
  }
  system("pause");
  return 0;
}

操作自定义类型

#include<iostream>
#include<queue>
#include<vector>
#include<string>
using namespace std;
class MM
{
public:
  MM(int age, string name): age(age), name(name) {}
  string getName() const
  {
    return name;
  }
  int getAge() const
  {
    return age;
  }
  bool operator < (const MM& object) const
  {
    return this->age < object.age;
  }
  friend ostream& operator << (ostream out, MM& object1) 
  {
    out << object1.age << object1.name;
    return out;
  }
private:
  int age;
  string name;
};
int main()
{
  priority_queue<MM, vector<MM>, less<MM>> date1;
  date1.push(MM(10, "温柔了岁月"));
  date1.push(MM(78, "温柔"));
  while (!date1.empty())
  {
    cout << date1.top().getName() << date1.top().getAge(); //不使用运算符<<重载,可以在写一个访问的函数去调用他
    date1.pop();
  }
  system("pause");
  return 0;
}

自己通过仿函数写个比较的方法

#include<iostream>
#include<vector>
#include<queue>
#include<string>
using namespace std;
class MM
{
public:
  MM(){}
  MM(int age, string name): age(age), name(name){}
  int getAge() const { return age; }
  string getName() const { return name;}
private:
  int age;
  string name;
};
class compareName  //仿函数
{
public:
  bool operator() (const MM& object1, const MM& object2) const
  {
    return object1.getName() > object2.getName();
  }
};
int main()
{
  priority_queue<MM, vector<MM>, compareName> date1;
  date1.push(MM(10, "温柔了岁月"));
  date1.push(MM(18, "月"));
  while (!date1.empty())
  {
    cout << date1.top().getName() << " " <<  date1.top().getAge();
    date1.pop(); 
  }
}


相关文章
|
5天前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
12 1
|
12天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
14天前
|
C++ 容器
STL—map容器
STL—map容器
|
18天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
26天前
|
C++ 容器
约瑟夫经典问题C++,STL容器queue解法
约瑟夫经典问题C++,STL容器queue解法
14 0
|
1月前
|
容器
|
3天前
|
监控 Kubernetes Docker
【Docker 专栏】Docker 容器内应用的健康检查与自动恢复
【5月更文挑战第9天】本文探讨了Docker容器中应用的健康检查与自动恢复,强调其对应用稳定性和系统性能的重要性。健康检查包括进程、端口和应用特定检查,而自动恢复则涉及重启容器和重新部署。Docker原生及第三方工具(如Kubernetes)提供了相关功能。配置检查需考虑检查频率、应用特性和监控告警。案例分析展示了实际操作,未来发展趋势将趋向更智能和高效的检查恢复机制。
【Docker 专栏】Docker 容器内应用的健康检查与自动恢复
|
2天前
|
NoSQL Redis Docker
Mac上轻松几步搞定Docker与Redis安装:从下载安装到容器运行实测全程指南
Mac上轻松几步搞定Docker与Redis安装:从下载安装到容器运行实测全程指南
12 0
|
3天前
|
存储 安全 数据库
【Docker 专栏】Docker 容器内应用的状态持久化
【5月更文挑战第9天】本文探讨了Docker容器中应用状态持久化的重要性,包括数据保护、应用可用性和历史记录保存。主要持久化方法有数据卷、绑定挂载和外部存储服务。数据卷是推荐手段,可通过`docker volume create`命令创建并挂载。绑定挂载需注意权限和路径一致性。利用外部存储如数据库和云服务可应对复杂需求。最佳实践包括规划存储策略、定期备份和测试验证。随着技术发展,未来将有更智能的持久化解决方案。
【Docker 专栏】Docker 容器内应用的状态持久化
|
3天前
|
机器学习/深度学习 监控 Kubernetes
【Docker 专栏】Docker 容器内服务的自动扩展与缩容
【5月更文挑战第9天】本文探讨了Docker容器服务的自动扩展与缩容原理及实践,强调其在动态业务环境中的重要性。通过选择监控指标(如CPU使用率)、设定触发条件和制定扩展策略,实现资源的动态调整。方法包括云平台集成和使用Kubernetes等框架。实践中,电商平台和实时数据处理系统受益于此技术。注意点涉及监控数据准确性、扩展速度和资源分配。未来,智能算法将提升扩展缩容的效率和准确性,成为关键技术支持。
【Docker 专栏】Docker 容器内服务的自动扩展与缩容