Measure Twice, Cut Once - Standard Template Library - Technology Tutorial - Tutorial

ENT, March 8, 2000 by Eric Binary Anderson

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 through 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 your 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 pointer 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 programmers 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 predefined 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. 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. Define an assignment operator for any object you wish, to use in STL, if that object allocates memory, or 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. To enable viewing of an STL string when you hover over the item in the debugger, add the following line -- all on one line -- to the AutoExp.dat file in one of your Visual Studio directories: std::basic_string[less than]char,std::char_traits[less than]char[greater than],std::allocator[less than]char[greater than][greater than]=[less than]_Ptr,s[greater than]

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 ; you really should 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.

COPYRIGHT 2000 Boucher Communications, Inc.
COPYRIGHT 2000 Gale Group
 

BNET TalkbackShare your ideas and expertise on this topic

Please add your comment:

  1. You are currently: a Guest |
  2.  

Basic HTML tags that work in comments are: bold (<b></b>), italic (<i></i>), underline (<u></u>), and hyperlink (<a href></a)

advertisement
CXO UnpluggedSmart Business interviews on BNET

See and hear how senior level executives across the Asia Pacific are developing smart business ideas across a variety of sectors. The focus is on the future, and on how businesses need to evolve.

advertisement
  • Click Here
  • Click Here
  • Click Here
advertisement

Content provided in partnership with Thompson Gale