Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;后进先出队列,从先进先出到后进先出的简单示例_C++_Stack_Queue_Lifo - Fatal编程技术网

C++ C++;后进先出队列,从先进先出到后进先出的简单示例

C++ C++;后进先出队列,从先进先出到后进先出的简单示例,c++,stack,queue,lifo,C++,Stack,Queue,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");

我如何将其转换为后进先出队列? 有什么简单的方法可以做到这一点吗? 这是一个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;
}
使用名称空间std;
int main(){
队列q;

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

#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
#包括
#包括
使用名称空间std;
int main()
{
堆栈q;

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

#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
#包括
#包括
使用名称空间std;
int main()
{
堆栈q;

不能使用
stack
代替队列吗?是的。只需将
std::queue
替换为
std::stack
。请注意:
std::queue::front()
->
std::stack::top()
使用
堆栈
代替队列?是的。只需将
std::queue
替换为
std::stack
。请注意:
std::queue::front()
->
std::stack::top()