【无标题】

  • C++中对栈的相关函数进行操作时,一定要清楚该函数的返回值是什么,官方的函数定义如下
void pop() noexcept(noexcept(c.pop_back())) /* strengthened */ {
         c.pop_back();
}
  • 从官方的函数定义中可以得知该函数的返回值是void类型,因此执行下列代码会报错,报错类型为 C2440 "=" 无法从void转换为"int"
#include 
#include 
using namespace std;
int main()
{
    stack op;
    op.push(1);
    int res;
    res = op.pop();
}
  • 因此想要正确地获取栈的栈顶元素,需使用top()函数,该函数会返回一个对应数据类型是数据,对其进行接受即可,如下面代码即为正确的代码
#include 
#include 
using namespace std;
int main()
{
    stack op;
    op.push(1);
    int res;
    res = op.top();
    op.pop();
}

你可能感兴趣的:(C++学习,c++,算法,开发语言)