Measure Twice, Cut Once
Woodworkers live by the saying "measure twice, cut once." In other words, if you cut a board too short, you don’t get a second chance to cut it longer. Craftsmen have long known that its worth the effort to spend time in thought up front, rather than dealing with the cost of immediate gratification later. The same is true of programming languages. C is a simple programming language, but it has many difficult long-term maintenance problems. C++ is a more complex language that has the potential to simplify the development process. C++ adopted the Standard Template Library (STL) as part of its standard. STL requires more up-front knowledge, but it pays off by achieving correct results more quickly and safely than C++ alone.
When a woodworker needs to perform a tricky or dangerous operation repeatedly, he builds a template known as a jig. The jig makes operations like cutting a circle with a saber saw simple and repeatable. C++, through its template feature, enables the crafty programmer to create programming jigs. You can think of the STL as the ultimate collection of completed, tested, and extensible programming jigs. Originally developed at Hewlett-Packard Co., the STL is a collection of C++ source code templates that can automate almost all of your data-structure programming.
STL is composed of four major types of templates: containers, iterators, algorithms, and functions. All four types work together to achieve the goal of faster, more correct coding. STL containers provide a number of ways to group objects of the same size together, depending on your access requirements. All STL containers can grow dynamically. In the same way a C pointer can move through the elements of an array using the ++ and -- operators, an STL iterator can move through the elements of an STL collection. In most ways, STL iterators appear to be pointers to items in an STL collection.
Because STL iterators act like pointers and provide a specific interface to an STL collection, the library can provide a number of algorithms that will work on any collection. STL algorithms are coding jigs that can usually work on any type of STL collection containing any type of user-defined objects. Algorithms provide features such as sorting, searching, and item-by-item calculations. Finally, STL functions take the place of C-style function pointers, adding speed and type-safety to operations such as comparing two objects.
STL is designed to be fast. Programmers often eschew software libraries, using performance as an excuse. To keep programmers from avoiding STL due to speed concerns, the library is designed to provide data structures and algorithms that perform as well as hand-tuned C code. Taken as a whole, STL provides an extremely efficient way to create collections for objects and perform dozens and dozens of pre-defined algorithms on those collections.
STL, however, is not perfect. The syntax, while consistent, is ugly and often confusing. Most compilers still have some trouble reporting useful errors on template code, so the errors you’ll encounter are often verbose and cryptic. STL collections are opaque, and within a debugger it is often difficult to determine the precise contents of a collection. Finally, some STL implementations are not efficient. Do some profiling to ensure that the STL implementation you choose performs as advertised -- especially the string implementation.
Here are a couple of tips from my experience. STL collections work best when they are collections of objects, rather than collections of pointers to objects. In the first case, STL will efficiently allocate a block of objects at a time and completely manage the memory allocation. In the second case, the allocation and management of the objects is up to you. Additionally, you should define an assignment operator for any object you wish to use in STL, if that object allocates memory. Otherwise you’ll see bugs whenever the STL container grows too large. When STL containers expand beyond their current memory size, they copy their contents item-by-item to a new block of memory, using the defined assignment operator.
Perhaps you already use STL regularly, and despite its obvious benefits, you are aggravated by the fact that you can’t view STL objects easily in the Microsoft VC++ debugger. To enable viewing of an STL string when you hover over the item in the debugger, use this rarely documented feature: Add the following line -- all on one line -- to the AutoExp.dat file in one of your Visual Studio directories:
std::basic_string<char,std::char_traits <char>,std::allocator<char>>=<_Ptr,s>
For unicode strings, add the line again, but substitute "unsigned short" for "char" and ",su" for ",s".
STL enables programmers to crank out solutions like a craftsman cranking out furniture -- safely and predictably. If you are using C++ as your primary development tool, you owe it to yourself and your company to learn about STL. --Eric Binary Anderson is a development manager at PeopleSoft’s PeopleTools division (Pleasanton, Calif.) and has his own consulting business, Binary Solutions. Contact him at ebinary@yahoo.com.