![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Comments The FIFO, or queue, data model is heavily used in task-oriented programming, such as a print manager. Usually, a single printer serves several users. Because the printer can handle only a single job at a time, the other print jobs have to wait their turn. This is the role of a print managerit stores incoming print jobs, sends them one-by-one to the printer, and waits for a notification that the printer has completed the current job. Only when the printer is free will the print manager send a new job to it. Every print job has a unique ID assigned to it by the print manager. The waiting jobs are stored in a queue. When the print manager receives a completion notification from the printer, it deletes this job from the queue. A print manager uses a queue to hold the pending jobs. The first job inserted to the queue is also the first to be printed and removed from it. Implementing a print manager requires knowledge of event-driven programming and serial port communication. Therefore, it is left as an exercise for readers who are experienced in such programming tasks. Summary In this chapter, you have learned how to use STL containers. First, the theoretical aspects of generic programming were discussed. Next, you implemented various data models: vector, string, stack, list, and queue. As you might have noticed, they share some common features such as automatic memory management, the notion of iterators that behave like pointers, begin() and end() member functions, and more. Still, every container is an abstraction of a distinct data model. The rich collection of STL containers enables you to pick up the most suitable container that fits your programming tasks. STL containers have several advantages over homemade ones:
The Standard Library has a few more almost containers. These template classes behave in many ways like ordinary containers: they have automatic memory management, they have iterators, and they have member functions such as begin() and end(). Still, they are not considered first class citizens in the STL catalog because they are not generic. You saw an example of that in the shape of string, which is similar to vector but is confined to the char data type. Valarray is another almost container that was not discussed. It also resembles vector, albeit with a strong bias toward numerical and mathematical computations. The third class in this category is bitset. Bitset is a container designed to store and manipulate bits in an efficient way. These almost containers have a limited use for general purposes, except for string. STL has a few more containers that were not discussed here. These containers include associative arrays, sets, and multisets. An associative array is an array whose indexes neednt be integers. The map<> container is an example of this. It can take any data type as a subscript for its elements. A map element is a pair consisting of a key and a value. For example, a URL serving as a key and a corresponding HTML page as its value. However, associative array elements have to meet other requirements so that they can be sorted and compared to one another. This is usually achieved by defining a functor, or a function object. Multimap<> is a map that can hold nonunique keys. Likewise, multiset<> is a set that can hold nonunique elements. Readers who wish to learn more about these advanced data models are referred to the following titles: STL Tutorial and Reference Guide: C++ Programming with the Standard Template Library, David R. Musser and Atul Saini. Reading, MA: Addison Wesley Longman (1996). (ISBN: 0201633981) The C++ Programming Language, 3rd ed., Bjarne Stroustrup. Reading, MA: Addison Wesley Longman (1997). (ISBN: 0201889544)
|
![]() |
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.
|