class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
int a;
if(stack2.empty()){
while(!stack1.empty()){
a=stack1.top();
stack2.push(a);
stack1.pop();
}
}
a=stack2.top();
stack2.pop();
return a;
}
private:
stack<int> stack1;
stack<int> stack2;
};
};
void没有返回值
vector,向量,动态数组,顺序访问
要使用头文件#include< vector >
stack1.push_back(node);//在数组最后添加数据,pop_back相反
stack1.size()是这个数组的大小
stack< int > a;
a.push(1); // 1
a.push(2); // 1 2
a.push(3); // 1 2 3
int c = a.top(); // c = 3
a.pop(); // 1 2
a.push(4); // 1 2 4
c = a.top(); // c = 4
while 循环的执行顺序非常简单,它的格式是:
while (表达式)
{
语句;
}
当表达式为真,则执行下面的语句;语句执行完之后再判断表达式是否为真,如果为真,再次执行下面的语句;然后再判断表达式是否为真…… 就这样一直循环下去,直到表达式为假,跳出循环。这个就是 while 的执行顺序。
pop_back ()&push_back (elem) 实例在容器最后移除和插入数据
#include <string.h>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int>obj;// 创建一个向量存储容器 int
for(int i=0;i<10;i++) //push_back (elem) 在数组最后添加数据
{
obj.push_back(i);
cout<<obj[i]<<",";
}
for(int i=0;i<5;i++)// 去掉数组最后一个数据
{
obj.pop_back();
}
cout<<"\n"<<endl;
for(int i=0;i<obj.size();i++)//size () 容器中实际数据个数
{
cout<<obj[i]<<",";
}
return 0;
}
输出结果为:
0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,
栈先进先出,队列先进后出