Presentation is loading. Please wait.

Presentation is loading. Please wait.

STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.

Similar presentations


Presentation on theme: "STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially."— Presentation transcript:

1 STL – Standard Template Library L. Grewe

2 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially included in the C++ Standard Library. It provides containers (data structures), iterators, algorithms and functions.

3 3 STL data structures include: vector: Dynamic-array-backed –const-time random-access –const-time insert/delete at back –Log-time search if sorted list: doubly-linked list –fast insert/delete anywhere multiset: non-sequential, non-unique –Bag set: non-sequential, unique

4 4 Vectors vector nums; //notice the use of Templates!!!! vector vals(20); –init=-size-20 vector of doubles vector objs;

5 More (containers) data structures in STL vector a dynamic array, like C array (i.e., capable of random access) with the ability to automatically resize itself when inserting or erasing an object. Inserting and removing an element to/from back of the vector at the end takes amortized constant time. Inserting and erasing at the beginning or in the middle is linear in time. A specialization for type bool exists, which optimizes for space by storing bool values as bits.Carrayrandom accessamortizedbool list a doubly-linked list; elements are not stored in contiguous memory. Opposite performance from a vector. Slow lookup and access (linear time), but once a position has been found, quick insertion and deletion (constant time).doublylinked list dequedeque (double ended queue)queue a vector with insertion/erase at the beginning or end in amortized constant time, however lacking some guarantees on iterator validity after altering the deque. stack, priority_queue, queue 5 Ordered data structures

6 More (containers) data structures in STL set a sorted set; inserting/erasing elements in a set does not invalidate iterators pointing in the set. Provides set operations union, intersection, difference, symmetric difference and test of inclusion. Type of data must implement comparison operator < or custom comparator function must be specified. Implemented using a self-balancing binary search tree.setunionintersectiondifferencesymmetric differenceself-balancing binary search tree multiset same as a set, but allows duplicate elements. map a sorted associative array; allows mapping from one data item (a key) to another (a value). Type of key must implement comparison operator < or custom comparator function must be specified. Implemented using a self-balancing binary search tree.associative array multimap same as a map, but allows duplicate keys. hash_set hash_multiset hash_map hash_multimap similar to a set, multiset, map, or multimap, respectively, but implemented using a hash table; keys are not sorted, but a hash function must exist for key type. These containers are not part of the C++ Standard Library, but are included in SGI's STL extensions, and are included in common libraries such as the GNU C++ Library in the __gnu_cxx namespace. These are scheduled to be added to the C++ standard as part of TR1, with the slightly different names of unordered_set, unordered_multiset, unordered_map and unordered_multimap.hash tablehash functioncontainersTR1unordered_map 6 Un-Ordered data structures

7 7 STL class creation and population…vector example STL Template class similar to bag vector nums; nums.insert(8); nums.insert(1);... vector nums; nums.insert(8); nums.insert(1);...

8 8 STL Iterators Standard way to traverse through container: iteration Abstraction of both “index” and “pointer” –just: means of iterating –forward, back, etc.

9 9 The for-loop with the iterator Can also use array syntax –But it works with all STL containers for (vector ::iterator curs = nums.begin(); curs != nums.end(); ++curs) { cout << *curs; } for (vector ::iterator curs = nums.begin(); curs != nums.end(); ++curs) { cout << *curs; }

10 10 Sample of STL Algorithms void sort(begin it, end it) –it-s must be random-access –members must support ==, < void random_shuffle(begin it, end it) –same req’s bool binary_search(begin, end, target) –same req’s –also: assumes sorted min_element(v.begin(), v.end()) –returns iterator Work with all data structures (containers)!

11 11 STL summary Very useful/convenient in real life Implements many of the DSs we study here –Important to understand anyway! NOTE: STL containers are not intended to be used as base classes (their destructors are deliberately non-virtual). Deriving from a container is a common mistake made by novices. [1][2]containers container [1][2]


Download ppt "STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially."

Similar presentations


Ads by Google