C++/Queue Stack/queue
Содержание
Put string into a queue
#include <iostream>
#include <string>
#include <queue>
#include <stack>
using namespace std;
int main()
{
cout << "FIFO order:\n";
queue<string> q;
q.push("A");
q.push("B");
q.push("C");
stack<string> s;
while (q.size() > 0)
{
string name = q.front();
q.pop();
cout << name << "\n";
s.push(name);
}
cout << "LIFO order:\n";
while (s.size() > 0)
{
cout << s.top() << "\n";
s.pop();
}
return 0;
}
queue.front()
#include <iostream>
using std::cout;
using std::endl;
#include <queue>
int main()
{
std::queue< double > values;
values.push( 3.2 );
values.push( 9.8 );
values.push( 5.4 );
cout << "Popping from values: ";
while ( !values.empty() )
{
cout << values.front() << " ";
values.pop();
}
cout << endl;
return 0;
}
/*
Popping from values: 3.2 9.8 5.4
*/
Queue of string: push(), empty(), front(), pop()
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main()
{
queue<string> q;
q.push( "A" );
q.push( "B" );
q.push( "C" );
while ( !q.empty() ) {
cout << q.front() << " ";
q.pop();
}
return 0;
}
Queue: push, pop and size
#include <iostream>
#include <queue>
#include <list>
using namespace std;
int main()
{
int thedata[] = {45, 34, 56, 27, 71, 50, 62};
queue<int, list<int> > q;
cout << "The queue size is now " << q.size() << endl;
cout << "Pushing 4 elements " << endl;
for (int i = 0; i < 4; ++i)
q.push(thedata[i]);
cout << "The queue size is now " << q.size() << endl;
cout << "Popping 3 elements " << endl;
for (int i = 0; i < 3; ++i) {
cout << q.front() << endl;
q.pop();
}
cout << "The queue size is now " << q.size() << endl;
return 0;
}
/*
The queue size is now 0
Pushing 4 elements
The queue size is now 4
Popping 3 elements
45
34
56
The queue size is now 1
*/
queue: push, pop, front and size
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main()
{
queue<string> q;
// insert three elements into the queue
q.push("These ");
q.push("are ");
q.push("more than ");
// read and print two elements from the queue
cout << q.front();
q.pop();
cout << q.front();
q.pop();
// insert two new elements
q.push("four ");
q.push("words!");
// skip one element
q.pop();
// read and print two elements
cout << q.front();
q.pop();
cout << q.front() << endl;
q.pop();
// print number of elements in the queue
cout << "number of elements in the queue: " << q.size()
<< endl;
}
/*
These are four words!
number of elements in the queue: 0
*/
queue with doubles
#include <iostream>
using std::cout;
using std::endl;
#include <queue>
int main()
{
std::queue< double > values;
values.push( 3.2 );
values.push( 9.8 );
values.push( 5.4 );
cout << "Popping from values: ";
while ( !values.empty() )
{
cout << values.front() << " ";
values.pop();
}
cout << endl;
return 0;
}
/*
Popping from values: 3.2 9.8 5.4
*/