Download presentation
Presentation is loading. Please wait.
Published byDennis Brett Gibson Modified over 8 years ago
1
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores other objects (elements) and provides methods for accessing its elements. Iterators Pointer-like objects used to access elements Algorithms Basic algorithms to manipulate the elements of containers. Examples include sort, find, reverse.
2
2 Containers Three categories of containers: Sequential (vector, deque, list) Its elements are stored in a linear order. Associative (map, set, hash, etc) Its elements are stored by (unique) key Its elements are retrieved by key. Retrieval is efficient. Supports insert/delete but not at specific locations. Adaptors (stack, queue, priority queue) Built on top of other containers.
3
3 Iterators An iterator is a generalization of a pointer It is used to iterate over the elements stored in a container. Usually invalidated after insert/delete operations Different classes have different types of iterators: forward (++) e.g. hash table iterators bidirectional (++/--) e.g. list iterators random access e.g. vector iterators
4
4 STL Vectors Similar to arrays, but number of elements can vary dynamically. Implemented like the dynamic array we've seen before Support random access Support constant time insert/delete at the end Support linear insert/delete at all other points
5
5 STL Vectors #include Example: vector V(3,10); // vector of integers, initially containing three 10s vector ::iterator it; // iterator to members of a vector of integers for (it = V.begin(); it != V.end(); it++) cout << *it << “ “; // note the pointer-like syntax. returns an iterator to the first element returns an iterator just past the last element Output: 10 10 10
6
6 STL Vectors vector V; // vector of integers, default capacity 0. cout << "initial capacity: " << V.capacity() << "\n"; for (int i=0; i<5; i++) { V.push_back(i); cout << "size: " << V.size() << ", capacity: " << V.capacity() << "\n"; } Output: initial capacity: 0 size: 1, capacity: 1 size: 2, capacity: 2 size: 3, capacity: 4 size: 4, capacity: 4 size: 5, capacity: 8 When the number of elements (size) is the same as the vector's capacity, and we try to insert a new element, the capacity is doubled. You should always assume that when the capacity changes (which involves memory reallocation) or when an element is removed (which changes the positions of elements), any existing iterators do not necessarily point to what they did before.
7
7 STL Deques Support random access Support constant time insert/delete at both endpoints Support linear time insert/delete in the middle Insert/Erase operations invalidate iterators Implementations Circular array Array of pointers to fixed-size blocks How would insert work?
8
8 STL Lists Doubly-linked Support fwd/bwd traversal (bidirectional iterators), but no random access Insert operation does not invalidate iterators, but delete will invalidate the iterator pointing to the element that was deleted.
9
9 STL Lists #include Example: list L; L.push_back(10); L.push_front(0); L.insert(L.begin()+1, 12); // insert element in 2nd position L.sort(); // a stable sorting algorithm (meaning that the // relative positions of equal elements are maintained)
10
10 Stacks in the STL #include Built on deques by default LIFO Structure. No iterators! Example: stack S; S.push(10); S.push(0); int x = S.top(); S.pop();
11
11 Queues in the STL #include FIFO structure, no iterators. Example: queue > Q; Q.push(10); Q.push(0); int s = Q.size(); while (!Q.empty()) Q.pop(); queue based on a vector, rather than the default deque. make certain there's space between the two angle brackets.
12
12 Adaptors Stacks and Queues are "based" on other containers. What is the relationship between a stack and a deque? The stack is NOT a deque public inheritance is wrong The stack USES a deque use private inheritance The stack implementation can use the deque's member functions to perform operations in a controlled way, but a user of the stack does not have access to the deque members and thus cannot modify the stack in an illegal way.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.