1 / 16

Standard Template Library

Standard Template Library. Homework. List HW will be posted on webpage Due Nov 15. The STL includes:. Containers – a data structure which holds arbitrary data type Iterators – way of ‘browsing’ through a container

alena
Download Presentation

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. Standard Template Library

  2. Homework • List HW will be posted on webpage • Due Nov 15

  3. The STL includes: • Containers – a data structure which holds arbitrary data type • Iterators – way of ‘browsing’ through a container • Algorithms – operations which can be performed on any container (where it makes sense)

  4. Containers • vector • list • deque (double-ended queue) • set and multiset • map and multimap

  5. vector • Much like an array • Accessible with [] • Provides constant-time random access • Dynamically resizable • May be of any type.

  6. list • Changes size as elements are inserted or deleted • May be of any type.

  7. list syntax #include <list> using namespace std; void aFunc() { list<float> lst; lst.push_back(7.0); }

  8. Some list methods • Since list is a class, it has methods • push_back() • pop_back() • front() • back() • empty() • clear() • size() • resize() • capacity() • See www.sgi.com/tech/stl/list.html

  9. Iterator • Iterators are a convenient way to ‘browse’ • Every container offers a object.start() • Every container offers an object.end() • Every iterator supports ++ and - - (no matter what the structure of the container)

  10. List organization • From any car in the train you may go to the preceding or succeeding one • Use ++ or - - operation on the iterator

  11. Lists • #include <list> • A list is like a freight train • Cars in sequence, starting with 0 • Each car is a container for a type of data – e.g. an int, a float, a object

  12. Examine the elements of a list #include <list> using namespace std; int main() { list<float> lst; list<float>::iterator lp; for (lp = lst.begin(); lp != lst.end(); ++lp) { // do something here } return 0; }

  13. Algorithms • You must include the header #include <algorithm> using namespace std;

  14. Algorithms • Provides standard algorithms like sort and swap on any kind of data in the container, provided …

  15. Algorithms • Provides standard algorithms like sort and swap on any kind of data in the container, provided … • That prerequisites are defined (the < operator for sort)

  16. Sort algorithm example #include <list> #include <algorithm> #include <iostream> using namespace std; int main() { list<int> lst; for (int i = 0; i < 5; ++i) { int aNum = rand() % 100; lst.push_back(aNum); } lst.sort(); return 0; }

More Related