我的代码脱离了上下文。这是在堆栈已经填满之后。在从堆栈中弹出对象后,变量first始终从第一次传递的堆栈顶部获取正确的值,但变量second始终返回32.任何帮助都将很高兴。
stack<char> stack;
stack.pop();
int first=atoi(&stack.top());//convert character to int
stack.pop();
int second=atoi(&stack.top());//convert character to int
答案 0 :(得分:4)
要将'0'
到'9'
范围内的单个字符转换为整数值,只需减去'0'
即可。 atoi
期望一个以空字符结尾的字符串,因此不会在这里工作。