This commit is contained in:
tao363 2022-12-19 21:48:20 +08:00
parent bbbed5b14b
commit fe7564d54d

View file

@ -29,6 +29,9 @@ public:
/* 出栈 */
int pop() {
if(stack.empty()){
throw out_of_range("栈为空,不能执行: pop()");
}
int oldTop = stack.back();
stack.pop_back();
return oldTop;
@ -36,11 +39,17 @@ public:
/* 访问栈顶元素 */
int top() {
if(stack.empty()){
throw out_of_range("栈为空,不能执行: top()");
}
return stack.back();
}
/* 访问索引 index 处元素 */
int get(int index) {
if(stack.size() < index){
throw out_of_range("超出栈空间大小");
}
return stack[index];
}