So I have a problem I'm working on for my university class. Essentially the professor gives us a template in a header file and a main(), He asks us what would happen if we tried to "cout << s2.top();" and s2.pop(); after the multiple s2.push() and while loop (When the stack is empty). I already understand that after we cout the data in the stack, we pop() the last element until it is empty. Here is the code for stack.h:
所以我有一个问题,我正在为我的大学课程工作。基本上教授给我们一个头文件和一个main()的模板,他问我们如果我们试图“cout << s2.top();会发生什么?”和s2.pop();在多个s2.push()和while循环之后(当堆栈为空时)。我已经明白,在我们对堆栈中的数据进行cout之后,我们会弹出()最后一个元素,直到它为空。这是stack.h的代码:
#ifndef STACK_H
#define STACK_H
#include <vector>
using namespace std;
template <typename T>
class Stack
{
vector<T> container;
public:
Stack(): container() {}
void push(T x) { container.push_back(x); }
void pop() { if (container.size() > 0) container.pop_back(); }
T top(){ if (container.size() > 0) return container.back(); }
bool empty() { return container.empty(); }
};
#endif
Here is the main():
这是main():
#include <iostream>
#include <string>
#include "stack.h"
main()
{
Stack<int> s1;
s1.push(4);
s1.push(3);
s1.push(2);
s1.push(1);
while (!s1.empty()) {
cout << s1.top() << endl;
s1.pop();
}
Stack<string> s2;
s2.push("Yoda said ");
s2.push("something ");
s2.push("to write ");
while (!s2.empty()) {
cout << s2.top();
s2.pop();
}
s2.pop();
cout << s2.top();
cout << endl;
}
I know we will get a segmentation fault or something similar since were trying to access or pop() an empty stack. He wants us to ONLY change stack.h. I've already added:
我知道我们会在尝试访问或弹出()空堆栈时遇到分段错误或类似问题。他希望我们只改变stack.h。我已添加:
"void pop() { if (container.size() > 0) container.pop_back(); }"
“void pop(){if(container.size()> 0)container.pop_back();}”
And it will work for s2.pop().
它适用于s2.pop()。
My question is, when I try to add "if (container.size() > 0)" in the line:
我的问题是,当我尝试在行中添加“if(container.size()> 0)”时:
"T top(){ return container.back(); }"
“T top(){return container.back();}”
I still get a segmentation fault even though (at least I think) I'm checking the size before returning? How would i go about this one? Thanks in advance :)
即使(至少我认为)我在返回之前检查尺寸,我仍然会遇到分段错误?我怎么去这个?提前致谢 :)
2 个解决方案
#1
2
If control reaches }
of a function returning non-void, instead of reaching return
, the behaviour is undefined. Anything could happen.
如果控制到达返回非void的函数,而不是到达返回,则行为是未定义的。什么事情都可能发生。
You have to add else
to the .top()
, and use it to return some kind of default value (you probably want return {};
), or throw an exception, or prevent the function from returning in some other way.
你必须在.top()中添加else,并使用它来返回某种默认值(你可能想要返回{};),或抛出异常,或阻止函数以其他方式返回。
#2
1
Your
s2.pop();
cout << s2.top();
outside the loop is a) calling pop()
on an empty container (just silly). b) calling top()
on an empty container and using the result; bug.
循环之外是a)在空容器上调用pop()(只是傻)。 b)在空容器上调用top()并使用结果;错误。
Also T top(){ if (container.size() > 0) return container.back(); }
results in undefined behaviour when container is empty since then the function does not return a T
(which it always must).
另外T top(){if(container.size()> 0)返回container.back();当容器为空时导致未定义的行为,因为该函数不返回T(它总是必须)。
#1
2
If control reaches }
of a function returning non-void, instead of reaching return
, the behaviour is undefined. Anything could happen.
如果控制到达返回非void的函数,而不是到达返回,则行为是未定义的。什么事情都可能发生。
You have to add else
to the .top()
, and use it to return some kind of default value (you probably want return {};
), or throw an exception, or prevent the function from returning in some other way.
你必须在.top()中添加else,并使用它来返回某种默认值(你可能想要返回{};),或抛出异常,或阻止函数以其他方式返回。
#2
1
Your
s2.pop();
cout << s2.top();
outside the loop is a) calling pop()
on an empty container (just silly). b) calling top()
on an empty container and using the result; bug.
循环之外是a)在空容器上调用pop()(只是傻)。 b)在空容器上调用top()并使用结果;错误。
Also T top(){ if (container.size() > 0) return container.back(); }
results in undefined behaviour when container is empty since then the function does not return a T
(which it always must).
另外T top(){if(container.size()> 0)返回container.back();当容器为空时导致未定义的行为,因为该函数不返回T(它总是必须)。