Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

ISBN:
0-13-185757-6
© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

[Note: This tutorial is an excerpt (Sections 23.1) of Chapter 23, Standard Template Library (STL), from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.1112-1123. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

 

23.1.1 Introduction to Containers (Continued)

STL Container Header Files

The header files for each of the Standard Library containers are shown in Fig. 23.3. The contents of these header files are all in namespace std. [Note: Some older C++ compilers do not support the new-style header files. Many of these compilers provide their own versions of the header-file names. See your compiler documentation for more information on the STL support your compiler provides.]
 

Standard Library container header files
<vector>
<list>
<deque>
<queue> Contains both queue and priority_queue.
<stack>
<map> Contains both map and multimap.
<set> Contains both set and multiset.
<bitset>

Fig. 23.3 Standard Library container header files.

 
First-Class Container Common typedefs

Figure 23.4 shows the common typedefs (to create synonyms or aliases for lengthy type names) found in first-class containers. These typedefs are used in generic declarations of variables, parameters to functions and return values from functions. For example, value_type in each container is always a typedef that represents the type of value stored in the container.
 

typedef Description
value_type The type of element stored in the container.
reference A reference to the type of element stored in the container.
const_reference A constant reference to the type of element stored in the container. Such a reference can be used only for reading elements in the container and for performing const operations.
pointer A pointer to the type of element stored in the container.
iterator An iterator that points to the type of element stored in the container.
const_iterator A constant iterator that points to the type of element stored in the container and can be used only to read elements.
reverse_iterator A reverse iterator that points to the type of element stored in the container. This type of iterator is for iterating through a container in reverse.
const_reverse_iterator A constant reverse iterator that points to the type of element stored in the container and can be used only to read elements. This type of iterator is for iterating through a container in reverse.
difference_type The type of the result of subtracting two iterators that refer to the same container (operator - is not defined for iterators of lists and associative containers).
size_type The type used to count items in a container and index through a sequence container (cannot index through a list).

Fig. 23.4 typedefs found in first-class containers.

Performance Tip
Performance Tip 23.3
STL generally avoids inheritance and virtual functions in favor of using generic programming with templates to achieve better execution-time performance.
Portability Tip
Portability Tip 23.1
Programming with STL will enhance the portability of your code.

When preparing to use an STL container, it is important to ensure that the type of element being stored in the container supports a minimum set of functionality. When an element is inserted into a container, a copy of that element is made. For this reason, the element type should provide its own copy constructor and assignment operator. [Note: This is required only if default memberwise copy and default memberwise assignment do not perform proper copy and assignment operations for the element type.] Also, the associative containers and many algorithms require elements to be compared. For this reason, the element type should provide an equality operator (==) and a less-than operator (<).

Software Engineering Observation
Software Engineering Observation 23.3
The STL containers technically do not require their elements to be comparable with the equality and less-than operators unless a program uses a container member function that must compare the container elements (e.g., the sort function in class list). Unfortunately, some prestandard C++ compilers are not capable of ignoring parts of a template that are not used in a particular program. On compilers with this problem, you may not be able to use the STL containers with objects of classes that do not define overloaded less-than and equality operators.
Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12

Tutorial Index