Presentation is loading. Please wait.

Presentation is loading. Please wait.

Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.

Similar presentations


Presentation on theme: "Standard Template Library. Homework List HW will be posted on webpage Due Nov 15."— 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 using namespace std; void aFunc() { list 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 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 using namespace std; int main() { list lst; list ::iterator lp; for (lp = lst.begin(); lp != lst.end(); ++lp) { // do something here } return 0; }

13 Algorithms You must include the header #include 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 using namespace std; int main() { list lst; for (int i = 0; i < 5; ++i) { int aNum = rand() % 100; lst.push_back(aNum); } lst.sort(); return 0; }


Download ppt "Standard Template Library. Homework List HW will be posted on webpage Due Nov 15."

Similar presentations


Ads by Google