Chapter 27. Standardizing on the Standard Template Library
In This Chapter
Using the string class
Maintaining entries in a Standard Template Library list
Accessing container elements from an iterator
Using a map container
Some programs can deal with data as it arrives and dispense with it. Most programs, however, must store data for later processing. A structure that is used to store data is known generically as a container or a collection. (I use the terms interchangeably.) This book has relied heavily on the array for data storage so far. The array container has a couple of nice properties: It stores and retrieves things quickly. In addition, the array can be declared to hold any type of object in a type-safe way. Weighed against these advantages, however, are two large negatives.
First, you must know the size of the array at the time it is created. This requirement is generally not achievable, although you will sometimes know that the number of elements cannot exceed some "large value." Viruses, however, commonly exploit this type of "it can't be larger than this" assumption, which turns out to be incorrect. There is no real way to "grow" an array except to declare a new array and copy the contents of the old array into the newer, larger version.
Second, inserting elements anywhere within the array involves copying elements within the array. This is costly in terms of both memory and computing time. Sorting the elements within an array is even more expensive.
C++ now comes with the Standard ...
Get C++ For Dummies®, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.