Writing Your Own STL Container: Ray Lischner
Writing Your Own STL Container: Ray Lischner
Ray Lischner
[email protected]
Who Am I?
• Ray Lischner
• Author
● C++ in a Nutshell
● Learning C++
● STL Pocket Reference
• Oregon State University
What Is The STL?
• Sequence
● deque, list, vector
● array
• Associative
● map, set
● multimap, multiset
● unordered_map, unordered_set
● unordered_multimap, unordered_multiset
Common Standards
template<class T>
class slist {
...
};
Singly-Linked List Review
next
next
value
next
value
next
value
value
Inserting a Node
next
next
value
next
value
next
value
value
next
value
Erasing a Node
next
next
value
next
value
next
value
value
next
value
Design Decisions
• Sequence containers
• Constant complexity
• Member functions:
● front(), push_front(), pop_front()
● back(), push_back(), pop_back()
Container Adapters
• Stacks
● head is back
• Queue
● head is front
● tail is back
What Good Is a Tail?
• push_back()
• back_inserter iterator adapter
Container Size
• A: Optimize size()
• B: Optimize splice()
• C: Lazy-evaluation
• D: I have a better idea
More Important?
• A: Optimize size()
• B: Optimize splice()
• C: Lazy-evaluation
• D: I have a better idea
•Allocators
• allocate(number_of_items)
• construct(pointer, value_to_copy)
• deallocate(pointer, number_of_items)
• destroy(pointer)
Rebind
• Five flavors
● Input
● Output
● Forward
● Bidirectional
● Random Access
How Are Iterators Used?
• Insertion point
• Item(s) to erase
• Item(s) to copy
Insertion
next
next
value
next
value
next
value
value
next
iterator
value
Erasure
next
next
value
next
value next
value
value
next
iterator value
Iterator Design
iterator_base
operator==
operator!=
iterator const_iterator
Iterator Invalidation
• splice
• merge, merge_sort
• unique
• Relational and equality operators
For More Information
Ray Lischner
[email protected]