Lec2 - Lecture 2: The Standard Template Library PIC 10C...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 2: The Standard Template Library PIC 10C Todd Wittman 20.1 : Standard Template Library (STL) c The STL is a collection of data structures built into most C++ compilers. c You are already familiar with one of the STL data structures: vector. c The basic data structures you learned in PIC 10B are available in the STL. They are called containers . c We don't teach the STL in 10B because you learn more about the structures by building them yourself. c But since you're now in 10C. ..
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 STL Fundamental Containers c Most names are what we expect. <queue> priority_queue Max-heap <map> map Hash table <set> set Binary tree <queue> queue Queue <stack> stack Stack <list> list Linked list <vector> vector Dynamic array Library to #include STL Name Data Strucutre Similiarities c 1.) All STL containers are templated. set<string> words; list<Record> students; stack< vector<int> > numberLists; //Note the space in > > c 2.) All STL containers have an associated iterator . c The iterator acts like a pointer. We identify the iterator type by container<T>::iterator set<string>::iterator p = words.find("Homer"); list<Record>::iterator first = students.begin(); stack< vector<int> >::iterator iter = numberLists.end(); You can dereference the iterator just like a pointer: cout << *p;
Background image of page 2
3 Similarities c 3.) All STL containers have the following member functions available. int size() //Returns number of items in the container. iterator begin() //Returns iterator to first element. iterator end() //Returns iterator to "sentinel" node at end. bool empty() //Returns true if the container is empty. c For example, to print all the elements of a list called myList. list<string>::iterator iter = myList.begin(); while ( iter != myList.end() ) { cout << *iter; iter++; } c This code will work for any of the containers (map, set, deque). But you may not like the order it gives you the elements. Differences
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/18/2010 for the course PIC 10C taught by Professor Wittman during the Spring '10 term at UCLA.

Page1 / 9

Lec2 - Lecture 2: The Standard Template Library PIC 10C...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online