Technology Industry
Industry: Email Alert RSS FeedMeasure 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.
Most RecentTechnology Articles
- Google Becomes (Almost) Full-Fledged Telecom, Vonage, Skype, Others In Sites
- Google Android Will Increasingly Win According to Gartner [UPDATE: Palm...
- Microsoft, Sony Were Right, Consoles Are the Future. Where's Apple?
- AOL, the $200 Million Coming Disaster
- Intel to Pay AMD $1.25 Billion; the Antitrust Cost Keeps Rising
- More »
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.
CXO UnpluggedSmart Business interviews on BNET
Brought to you by CBS MoneyWatch.com
- Best- and Worst-Paid College Degrees
- 6 Things You Should Never Do on Twitter or Facebook
- How Much Sleep Do You Really Need?
- 6 Big Myths about Gas Mileage
Most Recent Technology Articles
- INTERVIEW WITH BEN BUTTERS, DIRECTOR OF EUROPEAN AFFAIRS AT EUROCHAMBRES : "A PERFECT ROAD MAP FOR EU CLUSTERS DOES NOT EXIST".
- AGENDA.(Brief article)(Conference notes)
- FIGHT AGAINST INTERNET PIRACY.
- INTERNET : AUTHORS' SOCIETIES URGE ACTION AGAINST PIRACY.
- TELECOMMUNICATIONS : BUSINESSEUROPE HOSTILE TO FURTHER CONTRACTUAL OBLIGATIONS.(Brief article)
Most Recent Technology Publications
Most Popular Technology Articles
- 3G: naughty or nice? PhoneErotica.com generates over 300 million hits per month, and rings up more minutes of use per month than MSN
- Business process re-engineering in the small firm: A case study
- What is precision air conditioning and why is it necessary?
- Optimizing of Trichoderma viride cultivation in submerged state fermentation
- Performance analysis of shell and tube heat exchanger using miscible system



