![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Steps
How It Works Lets examine the program carefully. The first source line in MAIN.CPP includes the definition of the iostream class library. Next, the definition of STLs 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 } }
|
![]() |
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.
|