Effective STL
- Length: 288 pages
- Edition: 1
- Language: English
- Publisher: Addison-Wesley Professional
- Publication Date: 2001-06-16
- ISBN-10: 0201749629
- ISBN-13: 9780201749625
- Sales Rank: #147778 (See Top 100 Books)
“This is Effective C++ volume three – it’s really that good.”
– Herb Sutter, independent consultant and secretary of the ISO/ANSI C++ standards committee
“There are very few books which all C++ programmers must have. Add Effective STL to that list.”
– Thomas Becker, Senior Software Engineer, Zephyr Associates, Inc., and columnist, C/C++ Users Journal
C++’s Standard Template Library is revolutionary, but learning to use it well has always been a challenge. Until now. In this book, best-selling author Scott Meyers ( Effective C++ , and More Effective C++ ) reveals the critical rules of thumb employed by the experts – the things they almost always do or almost always avoid doing – to get the most out of the library.
Other books describe what’s in the STL. Effective STL shows you how to use it. Each of the book’s 50 guidelines is backed by Meyers’ legendary analysis and incisive examples, so you’ll learn not only what to do, but also when to do it – and why.
Highlights of Effective STL include:
- Advice on choosing among standard STL containers (like vector and list), nonstandard STL containers (like hash_set and hash_map), and non-STL containers (like bitset).
- Techniques to maximize the efficiency of the STL and the programs that use it.
- Insights into the behavior of iterators, function objects, and allocators, including things you should not do.
- Guidance for the proper use of algorithms and member functions whose names are the same (e.g., find), but whose actions differ in subtle (but important) ways.
- Discussions of potential portability problems, including straightforward ways to avoid them.
Like Meyers’ previous books, Effective STL is filled with proven wisdom that comes only from experience. Its clear, concise, penetrating style makes it an essential resource for every STL programmer.
Written for the intermediate or advanced C++ programmer, renowned C++ expert Scott Meyers provides essential techniques for getting more out of the Standard Template Library in Effective STL, a tutorial for doing more with this powerful library.
STL is a hugely powerful feature of today’s C++, but one with a well-earned reputation for complexity. The book is organized into 50 tips that explore different areas of the STL. Besides providing a list of dos and don’ts, Meyers presents a lot of background on what works and what doesn’t with STL. Each tip is demonstrated with in-depth coding samples, many of which make use of two-color printing to highlight the most important lines of code. (Advanced developers will enjoy Meyers’s in-depth explanations, while those who are in a hurry can skip ahead to the recommended tip itself.)
A good part of this book involves using containers, like vectors and maps, which are built into STL. (Besides the standard built-in containers, the author also highlights recent additions to STL like B-trees, which are available as extensions from other vendors.) You’ll learn the best ways to allocate, add, change, and delete items inside containers, including associative containers like maps. You’ll also learn to avoid common pitfalls, which can result in writing code that is slow or just plain wrong.
Other areas covered in Effective STL cover getting the most out of the 100-plus STL algorithms that are bundled with this library. Meyers shows you how to choose the correct algorithm for sorting and other functions. (Even advanced developers will learn something here.) Sections on using function objects (called functors) round out the text. Meyers shows you when these classes make sense and the best ways to implement them. Besides specific tips, you’ll get plenty of general programming advice. A useful appendix shows the limitations of STL as implemented in Microsoft Visual C++ 6.0 and how to overcome them.
Overall, Effective STL is a really invaluable source of programming expertise on an essential aspect of today’s C++ for anyone who is using–or planning to use–STL in real production code. It is quite simply a must-have. –Richard Dragan
Topics covered:
- Introduction to advanced Standard Template Library (STL) programming techniques
- 50 tips and best practices for STL illustrated with sample tutorial code
- Choosing containers
- Efficient copying of elements inside containers
- Removing, erasing, and cleaning up items from containers
- Using custom allocators with STL containers
- Thread safety with STL
- Tips for programming with the STL vector and string classes (including reserving memory and calling legacy C/C++ code)
- Tips for associative containers (including comparing items, sorted vectors, and non-standard enhancements to STL)
- Tips for selecting and using STL iterator classes
- STL algorithms (including sorting, removing, and comparing items)
- Using functors with STL
- General tips for STL programming (including advice for choosing algorithms and understanding compiler diagnostic messages)
- String locales
- Overcoming STL imitations in Microsoft Visual C++ 6.0
Table of Contents
Item 1. Choose your containers with care
Item 2. Beware illusion of container-independent code
Item 3. Make copying cheap & correct for objects in containers
Item 4. Call empty instead of checking size() against zero
Item 5. Prefer range member functions to their single-element counterparts
Item 6. Be alert for C++’s most vexing parse
Item 7. When using containers of newed pointers, remember to delete pointers before container is destroyed
Item 8. Never create containers of auto_ptrs
Item 9. Choose carefully among erasing options
Item 10. Be aware of allocator conventions & restrictions
Item 11. Understand legitimate uses of custom allocators
Item 12. Have realistic expectations about thread safety of STL containers
Item 13. Prefer vector & string to dynamically allocated arrays
Item 14. Use reserve to avoid unnecessary reallocations
Item 15. Be aware of variations in string implementations
Item 16. Know how to pass vector & string data to legacy APIs.
Item 17. Use “swap trick” to trim excess capacity
Item 18. Avoid using vector
Item 19. Understand difference between equality & equivalence
Item 20. Specify comparison types for associative containers of pointers
Item 21. Always have comparison functions return false for equal values
Item 22. Avoid in-place key modification in set & multiset
Item 23. Consider replacing associative containers with sorted vectors
Item 24. Choose carefully between map::operator[] & map-insert when efficiency is important
Item 25. Familiarize yourself with nonstandard hashed containers
Item 26. Prefer iterator to const iterator, reverse_iterator & const_reverse_iterator
Item 27. Use distance & advance to convert container’s const_iterators to iterators
Item 28. Understand how to use reverse_iterator’s base iterator
Item 29. Consider istreambuf_iterators for character-by-character input
Item 30. Make sure destination ranges are big enough
Item 31. Know your sorting options
Item 32. Follow remove-like algorithms by erase if you really want to remove something
Item 33. Be wary of remove-like algorithms on containers of pointers
Item 34. Note which algorithms expect sorted ranges
Item 35. Implement simple case-insensitive string comparisons via mismatch or lexicographical compare
Item 36. Understand proper implementation of copy_if
Item 37. Use accumulate or for_each to summarize ranges
Item 38. Design functor classes for pass-by-value
Item 39. Make predicates pure functions
Item 40. Make functor classes adaptable
Item 41. Understand reasons for ptr_fun, mem_fun & mem_fun_ref
Item 42. Make sure less means operator<
Item 43. Prefer algorithm calls to hand-written loops
Item 44. Prefer member functions to algorithms with same names
Item 45. Distinguish among count, find, binary search, lower_bound, upper_bound & equal_range
Item 46. Consider function objects instead of functions as algorithm parameters
Item 47. Avoid producing write-only code
Item 48. Always #include proper headers
Item 49. Learn to decipher STL-related compiler diagnostics
Item 50. Familiarize yourself with STL-related web sites