home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Steps

1.  Change to your base source directory and create a new subdirectory named QUEUE.
2.  Start your source code editor and type the following code into a file named MAIN.CPP.
// demonstrating STL’s queue
#include <iostream>
#include <queue>
using namespace std;

void main()
{
 queue <int> iq; //instantiate a queue of ints

 iq.push(93); //insert the first element, it is the top-most one
 iq.push(250);
 iq.push(25);
 iq.push(10); //last element inserted is located at the bottom

 cout<<”currently there are “<< iq.size() << “ elements” << endl;

 while (!iq.empty() )
 {
   cout <<”the last element is: “<< iq.front() << endl;
	     //front returns the top-most element
   iq.pop(); //remove the top-most element
 }
}
3.  Save MAIN.CPP and compile your project.
If your compiler complains about the following #include statements in MAIN.CPP:
#include <queue>
#include <iostream>

comment out (or remove) the using directive just below these #include statements and change them to read:
#include <queue.h>
#include <iostream.h>
4.  Compile and link main.
5.  Run main.exe. The output should look like this:
currently there are 4 elements
the last element is: 93
the last element is: 250
the last element is: 25
the last element is: 10

How It Works

Let’s examine the program carefully. The first source line in MAIN.CPP includes the definition of the iostream class library. Next, the definition of STL’s queue class template is included. Both iostream and queue are declared in namespace std. The following using directive instructs the compiler where it should look for the definitions of iostream and queue.

#include <iostream>
#include <queue>
using namespace std;

The first statement inside main() declares iq as a queue of ints.

void main()
{
 queue <int> iq;

The following statement inserts the first element into iq. Recall that the first element always remains at the top of the queue until it is popped.

iq.push(93);

The next three statements insert additional elements into iq. Each element occupies one position below the previously inserted one.

iq.push(250);
iq.push(25);
iq.push(10); //last element inserted is located at the bottom

The following statement displays how many elements currently exist in iq. Again, use the member function size() for that purpose.

cout<<”currently there are “<< iq.size() << “ elements” << endl;

Now define a loop that displays the topmost element in iq, and then removes it until iq has been emptied. The following while statement checks whether iq contains more elements first. Recall that popping an empty queue is an error.

 while (!iq.empty() )
 {
   cout <<”the last element is: “<< iq.front() << endl; //front returns
							//the top-most element
   iq.pop(); //remove the top-most element
 }
}


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.