Download
stl standard template library n.
Skip this Video
Loading SlideShow in 5 Seconds..
STL – Standard Template Library PowerPoint Presentation
Download Presentation
STL – Standard Template Library

STL – Standard Template Library

181 Views Download Presentation
Download Presentation

STL – Standard Template Library

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. STL – Standard Template Library L. Grewe

  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. 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. Vectors <vector> • vector<int> nums; //notice the use of Templates!!!! • vector<double> vals(20); • init=-size-20 vector of doubles • vector<string> objs;

  5. More (containers) data structures in STL Ordered data structures

  6. More (containers) data structures in STL Un-Ordered data structures

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

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

  9. 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; }

  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. 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]