LeetCode 中程序的报错位置如下
for(int i=0; i<sz; i++){
char c=s.pop();
str.push_back(c);
}
执行结果:
编译出错
Line 14: Char 18: error: cannot initialize a variable of type ‘char’ with an rvalue of type ‘void’ char c=s.pop(); ^ ~~~~~~~1 error generated.
解决方法:
因为pop()
只负责清除栈顶元素,不负责返回,所以返回的是void
。应该先top()
写出栈顶元素的值,再pop()
弹出。
Note:
将 char 字符链接成 string 字符串的方法,使用 push_back 逐个追加字符。
for(int i=0; i<sz; i++){
char c=s.top();
s.pop();
str.push_back(c);
}