C++ C++;后进先出队列,从先进先出到后进先出的简单示例
我如何将其转换为后进先出队列? 有什么简单的方法可以做到这一点吗? 这是一个FIFO->FIFO先进先出队列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");
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()