STL Container Guidelines: Comprehensive Summary
Overview
This document summarizes the key guidelines from “Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library” by Scott Meyers. It provides practical advice for C++ programmers on using STL containers, algorithms, and related components effectively.
Core Container Selection Principles
Item 1: Choose Containers Carefully
- Understand the variety of STL containers available
- Consider both standard and non-standard containers
- Evaluate trade-offs between contiguous-memory and node-based containers
- Default choice: vector, but context matters
Item 2: Avoid Container-Independent Code
- Cannot generalize across sequence and associative containers
- Member functions differ significantly between categories
- Iterator requirements vary (random-access vs bidirectional)
- Exception safety and performance implications
Specific Container Guidelines
Sequence Containers (vector, string, deque, list)
Item 4: Use empty() Instead of size() > 0
- More efficient and idiomatic
- Avoids double lookup in many implementations
Item 5: Prefer Range Member Functions
- Use push_back() and range inserters
- Better performance and cleaner code
Item 13: Prefer vector and string
- Often superior to manual memory management
- Better exception safety
Item 14: Use reserve() for Known Sizes
- Pre-allocates memory to avoid reallocations
- Critical for performance
Item 17: Use “the swap trick”
- swap() with temporary to reduce capacity
- More efficient than erase operations
Associative Containers (set, map, etc.)
Item 19: Understand Equality vs Equivalence
- Equality: == comparison, symmetric
- Equivalence: defined by container’s comparison object
- Critical for associative containers
Item 23: Consider Sorted Vectors
- Better cache locality than trees
- Simpler implementation for some cases
Item 24: Choose between map::operator[] and insert()
- operator[]: creates default value if missing
- insert(): preserves existing value
- Different exception safety guarantees
Item-Specific Guidelines
Memory Management (Items 3, 7, 8, 9)
- Make copying cheap and correct
- Delete pointers before container destruction
- Never use auto_ptr in containers
- Choose erasing options carefully
Performance Optimization (Items 10, 11, 12, 14, 15, 16, 17, 18)
- Understand allocator conventions
- Be aware of string implementation variations
- Know how to pass container data to legacy APIs
- Avoid vector
for bit operations
Iterator and Algorithm Usage (Items 20-30)
- Prefer iterator to const_iterator
- Use distance and advance for conversions
- Understand algorithm requirements
- Use erase-remove idiom correctly
Advanced Topics (Items 31-50)
- Know sorting options thoroughly
- Learn to decipher compiler diagnostics
- Familiarize with STL-related websites
Best Practices Summary
- Container Selection: Match container type to use case requirements
- Iterator Management: Understand category differences and invalidation rules
- Memory Safety: Use appropriate smart pointers and cleanup strategies
- Performance: Profile before optimizing, use standard algorithms when possible
- Exception Safety: Design for correct behavior during exceptions
- Thread Safety: Understand container guarantees for concurrent access
- Code Clarity: Write self-documenting code with clear intent
- Testing: Thoroughly test container interactions and edge cases
Reference Resources
- SGI STL Documentation: http://www.sgi.com/tech/stl/
- STLport: http://www.stlport.org/
- Boost Libraries: http://www.boost.org/
Book Details:
- Title: Effective STL
- Author: Scott Meyers
- Pages: 198
- Processing Method: PDF text extraction via pdf-inspector
- Status: Successfully processed
- Knowledge Domain: C++, STL, containers, algorithms, best practices