From fe7564d54d2b8f522f60f620fb99fe8133830b69 Mon Sep 17 00:00:00 2001 From: tao363 <306236356@qq.com> Date: Mon, 19 Dec 2022 21:48:20 +0800 Subject: [PATCH] master --- codes/cpp/chapter_stack_and_queue/array_stack.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/codes/cpp/chapter_stack_and_queue/array_stack.cpp b/codes/cpp/chapter_stack_and_queue/array_stack.cpp index 96a751035..c733e8491 100644 --- a/codes/cpp/chapter_stack_and_queue/array_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_stack.cpp @@ -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]; }