C++queue队列与stack栈

来源:互联网 发布:1855美国大亨 知乎 编辑:程序博客网 时间:2024/05/22 10:43

queue队列:

调用头文件:

#include<queue>

using namespace std;

详细用法(部分):

queue<Type> k;      ------      定义一个queue的变量(定义时已经初始化)      例如: queue<int> k;

k.empty()      ------      查看是否为空范例,是的话返回1,不是返回0

k.push(i)      ------      从已有元素后面增加元素i(队伍大小不预设)

k.pop()      ------      清除第一个元素

k.front()      ------      显示第一个元素      例如n = k.front();

k.back()      ------      显示最后一个元素

k.size()      ------      输出现有元素的个数


#include<stdio.h>#include<queue>using namespace std;int main(void){queue<int> k[4];k[1].push(7);k[1].push(8);while(k[1].empty()==0){k[2].push(5);printf("%d\n", k[1].front());k[1].pop();}printf("%d\n", k[1].size());return 0;}


stack栈:

调用头文件:

#include<stack>

using namespace std;

详细用法(部分):

stack<Type> k;      ------      定义一个stack的变量(定义时已经初始化)      例如: stack<int> k;

k.empty()      ------      查看是否为空范例,是的话返回1,不是返回0

k.push(i)      ------      在栈的最前面增加元素i(栈的大小不预设)

k.pop()      ------      清除第一个元素

k.top()      ------      显示第一个元素      例如n = k.top();

k.size()      ------      输出现有元素的个数


#include<stdio.h>#include<stack>using namespace std;int main(void){stack<int> k[4];k[1].push(7);k[1].push(8);while(k[1].empty()==0){k[2].push(5);printf("%d\n", k[1].top());k[1].pop();}printf("%d\n", k[1].size());return 0;}


1 0
原创粉丝点击