1 / 12

The Standard Template Library

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

Télécharger la présentation

The Standard Template Library

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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. 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. 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. STL Vectors • #include <vector> • Example: vector<int> V(3,10); // vector of integers, initially containing three 10s vector<int>::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. STL Vectors vector<int> 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. 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. 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. STL Lists • #include <list> • Example: list<int> 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. Stacks in the STL • #include <stack> • Built on deques by default • LIFO Structure. No iterators! • Example: stack<int> S; S.push(10); S.push(0); int x = S.top(); S.pop();

  11. Queues in the STL • #include <queue> • FIFO structure, no iterators. • Example: make certain there's space between the two angle brackets. queue<int, vector<int> > 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.

  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.

More Related