1 / 11

STL – Standard Template Library

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.

Télécharger la présentation

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

More Related