The C++ Standard Library from Scratch

What follows is the complete introduction to The C++ Standard Library from Scratch, by Pablo Halpern. Please read this disclaimer from Que. For more information, please visit The C++ Standard Library from Scratch home page.

This chapter is also available in a Serbo-Croatian language translation thanks to the WHG Team.

Introduction

 
In this Chapter

What Makes This Book Different?

This book is different from any primer on this topic ever written. Here's the difference: All other programming books start by teaching you simple skills which build in difficulty, adding skill on skill as you go. When you've learned all the skills, the books then demonstrate what you can do: a sample program.

This book does not start with a list of classes and functions; it starts with a project. You begin by analyzing and designing the project and then you implement that design. Standard library features are taught in the context of implementation; first you understand what you are trying to accomplish, and then you learn the skills and library features needed to get the job done.

Learning the Standard Library Doesn't Have to Be Difficult

Many people find the C++ Standard Library to be intimidating. I believe this is because most books and courses focus on the set of features supplied by the library, rather than on the problem these features help you solve. The C++ Standard Library is so large that trying to learn it this way is like trying to learn French by memorizing a French dictionary.

There are two ways to learn to speak a foreign language. One way is to memorize dozens of vocabulary words and practice declension of verbs. The other is to go to the country and interact with native speakers. Different people learn differently, but I can tell you that in my experience a week in France is worth two years in the classroom.

If I were going to teach you the C++ Standard Library and we worked together, I would not hand you a book at all. I'd sit down with you and we'd write a program together. Along the way, I'd teach you what you need to know, occasionally giving you short pieces to read to flesh out your understanding.

That is exactly how this book works: we'll sit down together and write a program, and along the way I'll teach you what you need to know. From the very first page, you will focus on understanding the problem you are trying to solve, and designing a solution, rather than on a list of features.
 
 
Note Because this book might be the first book you read after learning C++, I have included a number of excursions that review C++ concepts. If you are an old hand at C++ programming, simply skip these excursions.

Learning to Make Choices Among Library Constructs

One drawback of the other teaching approaches is that you get bombarded with a set of features with little guidance on how to choose among them. Because you are working through a real program, I can explain alternative ways of solving a problem and describe why we choose a particular solution. Returning to the foreign language analogy, you will learn not only the legal way to use library constructs, but the idiomatic use of those constructs.

Geek SpeakAn Idiom is a customary way of using a programming construct or feature. The meaning of an idiomatic construct is understood as a whole, without having to analyze the individual parts.

Using idioms reduces the amount of thinking you have to do and it makes your code easier to maintain by other people who understand the idioms.

Why Should I Learn the C++ Standard Library?

The official standard for the C++ language is a 776-page long document (ISO/IEC 14882, 1998). About half of that document is devoted to the library of functions and classes that are provided with standard-conforming compilers. If you use C++ without understanding the Standard Library, you know only half the story.
 
 
Tip If you do not already know how to program in C++, put this book down right now. Run (do not walk) to the store where you bought this book and buy Jesse Liberty's C++ From Scratch (ISBN: 0-7897-2079-5). When you are finished reading C++ From Scratch, you can resume reading this book where you left off.

A large part of programming involves mundane tasks such as managing collections of objects, manipulating text strings, searching, sorting, and performing formatted input and output. Though conceptually simple, many of these tasks are tedious and some are hard to get right. Do you really want to implement a sort algorithm yourself? What are the chances of introducing a subtle bug that only shows up when, say, sorting arrays that contain three elements with the same value?

Proper use of the library increases the portability of your programs to different compilers and operating systems. The use of standard library classes for data interchange is becoming increasingly common as more and more third-party libraries use standard classes in their interfaces.

By learning about the Standard Library, you can avoid reinventing the wheel. The features of the Standard Library are there to make your programs easier to write and more robust. The people who maintain your code will thank you for using well-understood facilities instead of inventing your own. (Okay, they might not actually thank you, but they'll be grateful inside and at least they won't curse you.)

What Is the C++ Standard Library?

The C++ Standard Library is really two things. In the classical sense, it is a set of types and functions, which come ready-to-use with a standards-conforming compiler.

Geek SpeakA library is a set of reusable components (classes, functions, macros, and so on) intended to help a programmer perform a set of tasks. A library can be special purpose (for example, Internet programming) or general purpose (for example, sorting).

Geek SpeakA standard library is a library whose contents and behavior are described in a document issued by a standards body (for example, the International Standards Organization, ISO). Unless otherwise stated, "standard library" is used here as an easier way to say "C++ Standard Library" -- that is, the library described in the ISO C++ standard.

Geek SpeakA standards-conforming compiler is a compiler and library that correctly implements the behavior described in the ISO standard for the C++ language and library.

But the C++ Standard Library is also an extensible framework for creating additional library components. Most of your work with the Standard Library will not involve creating new components. But for those 10% of the cases where new components are needed, the power of the library proves to be extraordinary. It is the ability to create new library components that distinguishes skilled library users from novices. With this book, I hope to move you a substantial distance down the road towards being a skilled library user.

Parts of the Standard Library

Ten smaller libraries together comprise the C++ Standard Library. They are

  1. The Language Support Library, which contains types and functions directly related to the language and compiler. For example, there are constants that define the range and precision of floating point numbers and functions for exiting a program.
  2. The Diagnostics library, which contains exception classes and other facilities for detecting and reporting errors.
  3. The General Utilities Library, which contains useful facilities not easily categorized into one of the other libraries.
  4. The Strings Library, for manipulating text strings.
  5. The Localization Library, for handling international character sets and culturally specific formatting of things such as time, date, and currency.
  6. The Containers Library, which provides generic containers for collecting objects.
  7. The Iterators Library, for traversing items in a collection.
  8. The Algorithms Library, which implements many commonly used data-manipulation algorithms.
  9. The Numerics Library, which provides complex numbers, special numeric array types, and numeric algorithms.
  10. The Input/Output Library, for performing portable formatted and unformatted input and output to files and other devices.
Which Parts Are Really Useful?

The ten libraries aren't equally useful. Some libraries you will use every day, whereas others you might never use. This book concentrates on the five libraries that I consider most useful in day-to-day use: Strings, Containers, Iterators, Algorithms and Input/Output.

Conversely, not everything that could be useful was included in the Standard Library. The ISO (International Standards Organization) committee responsible for creating the International C++ Language Standard reviewed hundreds of proposals for features that could have been added to the standard. They attempted to include only those features that are of the most general use and that had well-constructed proposals.

In retrospect, many generally useful features were omitted, whereas other less generally useful features were included. As we go along, I will point out some of the more glaring omissions. I won't bore you with all the obscure features that are hardly ever used. In picking and choosing, I am in a sense second-guessing the ISO committee members that slaved over the standard for so long. This is my prerogative as an author with 20/20 hindsight and I make no apologies.

What About Those Library Features I Used to Use in C?

If you programmed in C before learning C++, you are probably familiar with a number of library functions that are part of the C Standard Library. These functions -- such as printf, strcpy, atoi, and so on -- are still available to you in the C++ Standard Library and you will explore a number of them in this book. Some parts of the C library, such as the input/output functions, are rarely used in C++ because more sophisticated components are available in the C++ library. Other parts of the C library, however, continue to be valuable for writing C++ programs.

The C Standard Library underwent a few changes when it was incorporated into the C++ Standard Library. To begin with, the header files have changed names. For example, stdio.h was renamed to cstdio. The rule is the same for the other headers: Remove the .h and put a lowercase c in front.

Taking advantage of the capability to overload function names in C++, some functions were changed to make better use of const. For example, the C function

char* strchr(const char* s, char c);

has been replaced in C++ with the following two overloaded functions:

const char* strchr(const char* s, char c);

char* strchr( char* s, char c);

The first form searches for a character within a array of const characters and returns a const pointer to the first one. The second form works for a non-const array and returns a non-const pointer. This closes a const-safety hole in the C version of the function, which returned a non-const pointer into a potentially const array.

Overloading also provides multiple versions of floating-point math functions for the different size floating-point number types. For example, the C function

double sin(double);

has been replaced in C++ with

float sin(float);

double sin(double);

and

long double sin(long double);

Finally, all components inherited from the C library except macros have been put into the std namespace. (The macros from the C library are still available but, since macros do not obey scoping rules, they cannot be assigned to a namespace.) We delve further into the std namespace in the next chapter.

Is the Standard Library the Same as STL?

STL stands for Standard Template Library. The term refers to a set of interfaces and components developed by Alexander Stepanov and others working at AT&T Bell Laboratories and Hewlett-Packard Research Laboratories. Stepanov's goal was to realize the potential of generic programming to create a standard set of reusable components. Today, the STL comprises the Containers, Iterators, Algorithms, and some of the General Utilities libraries within the C++ Standard Library. In other words, STL is only a part of the Standard Library, albeit a very important part. Much of this book is devoted to understanding and using STL components.

Geek SpeakAn interface is any formal description of how a programmer or user gains access to the capabilities of a function, class, macro, or any other component of the system. Interface is an abstract programming concept, not a specific keyword C++ construct.

Geek SpeakA class interface describes the set of public functions, types, and variables in a class.
 

Geek SpeakA function interface describes the set of argument types and the return type for a function (i.e., its prototype).
 

Design Goals of the STL

When the main ideas of the STL were presented to the C++ standardization committee in late 1993, they were received enthusiastically. So rare was it that a proposal could satisfy so many goals simultaneously. The STL successfully achieved the following design goals:

If you have been hearing a lot about the STL and were wondering what all of the excitement was about, now you know. Anything that can promise this much is bound to cause excitement.

What Tools Do I Need?

Theoretically, you could read this book without ever touching a computer. However, in order to get the most out of it, you're going to want to compile some or all of the sample code in the book, perhaps modifying it to experiment with the library features. I also suggest that you try to solve the exercises organized by chapter on the support web site, http://www.halpernwightsoftware.com/stdlib-scratch/support.html.

In order to compile and run the programs in this book, you will need a text editor or Integrated Development Environment (IDE), a modern C++ compiler, and the standard library itself.

What Compilers Will Work?

This book is based on the C++ language as standardized by the International Standards Organization (ISO) in July 1998. Theoretically, therefore, any standards-conforming compiler will work equally well. As of this writing, however, standards-conforming compilers are rare. Compilers vary as to which features of Standard C++ they have not yet implemented. Some compilers are so hopelessly behind that they cannot effectively support a commercial-grade version of the Standard Library. You should avoid any compiler that predates the December 1996 draft of the ISO standard. In fact, few compilers from before 1998 are usable with the standard library.

The examples in this book have been tested with the Microsoft Visual C++ 6.0 compiler on Windows and the GNU egcs 1.1.2 compiler on UNIX/Linux. The egcs compiler is the next-generation of the GNU compiler and is substantially closer to ISO conformance than the older GNU compiler. It is available for free on the Web at http://egcs.cygnus.com/. Note that the egcs compiler has been improved and is now called GCC 2.95.

The other popular compiler that should work for Microsoft Windows is the Borland compiler that comes with C++ Builder 4.0. (This is not the Borland C++ 4.0 compiler, which is quite old and insufficient for supporting the standard library.) On UNIX and other operating systems, the compilers from KAI and those derived from the Edison Group compiler have received favorable reviews for standards conformance.

Because Sun's Solaris operating system is a popular software development platform, it is worth singling out the SunPro 5.0 compiler as another compiler you can use for programming with the standard library. The previous version of the SunPro compiler, 4.2, is marginally usable for standard library programming with some workarounds.

Compiler Notes

Even among the compilers listed earlier, there are errors, flaws, and idiosyncrasies that differ from compiler to compiler. In cases where I can identify these idiosyncrasies, I will flag them for you using a note that looks like this:
 
 
Note Compiler Note: The XYZ compiler fails to recognize the #include directive. The work around is to concatenate all your source files and the standard library headers into a single, huge, monstrosity.

Some compiler notes will apply to a whole class of compilers (for example, pre-1998 compilers), whereas others will apply to a specific compiler. The examples in this book have not been compiled on every compiler on the market, or even on every compiler mentioned here. Take advantage of the tips in the Compiler Notes, but know that I have certainly missed many opportunities for enlightening you on potential compiler problems. If my Compiler Notes seem to be picking on the Microsoft compiler, it is only because it is the one on which I have done the most testing. The fact that Microsoft owns most of the world (or at least most of the market) also makes their compiler a reasonable subject for thorough coverage.

Where Do I Get the Standard Library?

Most of the compilers mentioned earlier come with their own implementation of the C++ Standard Library.
 
 
Note Compiler Note: The version of the Standard Library that comes with the Microsoft Visual C++ 6.0 and earlier compilers has some known bugs, at least one of which would prevent you from compiling the code in this book. Dinkumware, Ltd., the company that supplies the library to Microsoft, has fixed many of these bugs. A list of fixes is available on the Internet at http://www.dinkumware.com/vc_fixes.html. You can also purchase a complete, updated library through this Web site.

Some of the older compilers (for example, the SunPro 4.2 compiler) provide an implementation of streams but not of the STL or standard string class. You can get implementations for these parts of the library from various commercial and free sources, including those listed here.

Standard Template Library Adaptation Page

http://www.stlport.org/

The STLport project provides a port of the STL and standard string class for many different compilers at no cost. It is built on the commercial-grade SGI version of the library and should probably be the first place you look. Also contains links to other good sites.

ObjectSpace, Inc.

http://www.objectspace.com

ObjectSpace sells a number of class libraries for C++ and Java. Their very-portable version of the STL and strings library (Standards<Toolkit>) comes bundled with many of their C++ class libraries. ObjectSpace used to provide the library for free downloading, though that no longer seems to be the case.

Rogue Wave, Inc.

http://www.roguewave.com/

Like ObjectSpace, Rogue Wave sells a number of class libraries for C++, including a standard library. Beware that most of Rogue Wave's other products do not directly use the standard library, but instead use a proprietary library called Tools.h++, which is largely a set of wrapper classes designed to give STL classes an older-style, more limited interface. The Rogue Wave version of the standard library is bundled with a number of compilers.

Geek SpeakA wrapper is a class or function intended only to provide a different interface to another class or function and which provides little new functionality.

Conventions Used in This Book

Some of the unique features in this series include the following:

Geek SpeakThe Geek Speak icon in the margin indicates the use of a new term. New terms will appear in the paragraph in italics.
 
 
 
How too pro nounse' it How To Pronounce It You'll see an icon set in the margin next to a box that contains a technical term and how it should be pronounced. For example, "cin is pronounced see-in, and cout is pronounced see-out."

Excursion


These are short diversions from the main topic being discussed, and they offer an opportunity to flesh out your understanding of a topic.
Sometimes an excursion is titled The Gory Details. This kind of excursion is used to introduce details that you probably don't want to know, but which you will be better off knowing. In general, these gory details do not affect the sample program.

With a book of this type, a topic can be discussed in multiple places as a result of when and where we add functionality during application development. To help make this all clear, I've included a Concept Web that provides a graphical representation of how all the programming concepts relate to one another. You'll find it on the inside front cover of this book.
 
 
Note Notes give you comments and asides about the topic at hand, as well as full explanations of certain concepts.
Tip Tips provide great shortcuts and hints on how to program in C++ more effectively.
Warning Warnings warn you against making your life miserable and avoiding the pitfalls in programming.

In addition, you'll find various typographic conventions throughout this book:

Getting Project Code and Support

Support for this book is available on the Internet at http://www.halpernwightsoftware.com/stdlib-scratch/support.html. Here you will find source code, a quick reference guide, exercises, errata, and links to help you get the most out of this book. Check back periodically for new information and links.

The source code on the Web is organized by chapter, with the final project source in the Chapter 10 Code section. Look for Code Notes in the text, which will direct you to the code directory for each set of listings.

[ return to The C++ Standard Library from Scratch home page ]