C++ LIFO 队列,从 FIFO 到 LIFO 的简单示例

C++ LIFO queue, easy simple example from FIFO to LIFO

我怎样才能使它进入 LIFO-> 后进先出队列? 有什么简单的方法吗? 这是先进先出队列中的 FIFO-> fifo。

using namespace std;

int main(){
    queue<string> q;

    cout << "Pushing one two three four\n";
    q.push("one");
    q.push("two");
    q.push("three");
    q.push("four");

    cout << "Now, retrieve those values in FIFO order.\n";
    while(!q.empty()) {
        cout << "Popping ";
        cout << q.front() << "\n";
        q.pop();
    }
    cout << endl;

    return 0;
}

你可以使用堆栈,这是后进先出

#include <stack>
#include <string>

using namespace std;
int main()
{
    stack<string> q;

    cout << "Pushing one two three four\n";
    q.push("one");
    q.push("two");
    q.push("three");
    q.push("four");

    cout << "Now, retrieve those values in LIFO order.\n";
    while (!q.empty()) {
        cout << "Popping ";
        cout << q.top() << "\n";
        q.pop();
    }
    cout << endl;

    return 0;
}

Output:
Pushing one two three four
Now, retrieve those values in LIFO order.
Popping four
Popping three
Popping two
Popping one