110 likes | 320 Vues
STL – Standard Template Library. L. Grewe. 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.
E N D
STL – Standard Template Library L. Grewe
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.
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
Vectors <vector> • vector<int> nums; //notice the use of Templates!!!! • vector<double> vals(20); • init=-size-20 vector of doubles • vector<string> objs;
More (containers) data structures in STL Ordered data structures
More (containers) data structures in STL Un-Ordered data structures
STL class creation and population…vector example • STL Template class similar to bag vector<int> nums; nums.insert(8); nums.insert(1); ...
STL Iterators • Standard way to traverse through container: iteration • Abstraction of both “index” and “pointer” • just: means of iterating • forward, back, etc.
The for-loop with the iterator • Can also use array syntax • But it works with all STL containers for (vector<int>::iterator curs = nums.begin(); curs != nums.end(); ++curs) { cout << *curs; }
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)!
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]