1、栈容器简介
还是在 堆区,而不是在栈区,因为容器的内存空间都是动态分配和回收的,所以这些容器还是在 堆区,所以stack 容器也是在堆区
点击这里查看栈区和堆区的区别
2、栈容器 API 操作
3、栈容器的使用
#include <iostream>
#include <stack>
using namespace std;
void test01()
{
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
if(!s.empty())
{
cout<<"栈的大小:"<<s.size()<<endl;
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
}
}
#include<queue>
void test02()
{
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
q.push(50);
if(!q.empty())
{
cout<<"栈的大小:"<<q.size()<<endl;
while(!q.empty())
{
cout<<q.front()<<" ";
q.pop();
}
}
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
4、queue(队列容器)简介
用于购票服务等
5、queue API操作
6、queue的使用
#include<queue>
void test02()
{
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
q.push(50);
if(!q.empty())
{
cout<<"队列的大小:"<<q.size()<<endl;
while(!q.empty())
{
cout<<q.front()<<" ";
q.pop();
}
}
}
int main(int argc, char *argv[])
{
test02();
return 0;
}