将字符串放入堆栈C++时出错

Yod*_*oda 0 c++ stack

我的"程序语言"(C++)存在很大问题.我想打印一堆字符串.

void show(stack<string> stos) {
  while (!stos.empty()) {
    cout << stos.pop() << endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

jro*_*rok 5

pop()只从堆栈中删除顶部元素并将其抛弃.它返回void(没有),你cout显然无法用它打印.你需要:

void show(stack<string> stos)
{
    while(!stos.empty()) {
        cout << stos.top() << endl;
        stos.pop();
    }
}
Run Code Online (Sandbox Code Playgroud)