Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 117,574 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 2,090 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



STL queues

 
Reply to this topicStart new topic

> STL queues, A simple "how to"

gabehabe
Group Icon



post 14 Jul, 2008 - 04:44 AM
Post #1


STL Container: Queue

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.
cpp
#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:
cpp
    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)
Notice the similarities to a queue in a shop? Basically, queue follows a "first come, first serve" pattern.
cpp
    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:
cpp
    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:
cpp
#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 smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/7/08 08:48PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month