Queues... what the hell are they?
Queues are probably the most basic STL container. They don't really have too many commands, and they aren't very flexible. You can probably guess where they get their name from... think of a queue in a shop, they do about the same thing.
Starting our program
First off, we need to include the right stuff. All we need to include is #include <queue> and be using std::queue; but I'm gonna be using #include <iostream> just so that you can see what's happening.
#include <iostream>
#include <queue>
using namespace std;
int main ()
{
Creating a queue and adding values
Very simple to create, pretty much the same as any other STL creation. Also note that we use push() to add something to the queue... this does pretty much the same thing as push_back() would in a vector:
queue <string> names; /* Declare a queue */
names.push ("Danny"); /* Add some values to the queue */
names.push ("Kayleigh"); /* Much like vectors */
names.push ("Joe"); /* This basically does the same thing */
Simple, huh? Damn straight it is, and it doesn't get much harder, either.Accessing the stuff in a queue
Really simple yet again, all we do in this next bit is make calls to 3 function, all of which are self explanatory:
- size() returns the size of the queue (how many members are in it)
- front() accesses the front element of the queue (the first one added)
- back() accesses the back element of the queue (the last one added)
cout << "There are currently " << names.size () << " people in the queue" << endl
<< "The person at the front of the queue is " << names.front () << endl
<< "The person at the back of the queue is " << names.back () << endl << endl;
Removing an element from a queue
Now, remember how I said it follows a "first come, first serve" rule? This is where you see it in action. The front member in the queue (currently me, Danny) is going to be first out of the queue, right? This is simple enough too:
cout << names.front () << " has been served!" << endl;
names.pop ();
cout << "There are currently " << names.size () << " people in the queue" << endl
<< "The person at the front of the queue is " << names.front () << endl
<< names.back () << " is still at the back!" << endl;
Now, Danny (me) has been served and left the queue, so Kayleigh is now at the front, and Joe is still at the back.Oh, and don't forget to close off main: }
And that's all there is to it! Told you it was simple!
Note: There is no such thing as an iterator when working with queues.
Here is all of the code that I used in writing this tutorial:
#include <iostream>
#include <queue>
using namespace std;
int main ()
{
queue <string> names; /* Declare a queue */
names.push ("Danny"); /* Add some values to the queue */
names.push ("Kayleigh"); /* Much like vectors */
names.push ("Joe"); /* This basically does the same thing */
cout << "There are currently " << names.size () << " people in the queue" << endl
<< "The person at the front of the queue is " << names.front () << endl
<< "The person at the back of the queue is " << names.back () << endl << endl;
cout << names.front () << " has been served!" << endl;
names.pop ();
cout << "There are currently " << names.size () << " people in the queue" << endl
<< "The person at the front of the queue is " << names.front () << endl
<< names.back () << " is still at the back!" << endl;
cin.get ();
return EXIT_SUCCESS;
}
For more information, visit the queue reference over at http://www.cplusplus.com









MultiQuote


|