Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 The Standard Template Library Drozdek Section 3.7.

Similar presentations


Presentation on theme: "1 The Standard Template Library Drozdek Section 3.7."— Presentation transcript:

1 1 The Standard Template Library Drozdek Section 3.7

2 2 Objectives You will be able to use the Standard Template Library list class in C++ programs.

3 3 The Standard Template Library Big Subject Brief introduction today. We will use one template as an example: The list template

4 4 STL Components Containers: Generic "off-the-shelf" class templates for storing collections of data Algorithms: Generic "off-the-shelf" function templates for operating on containers Iterators: Generalized pointers that allow algorithms to operate on almost any container

5 5 The Ten STL Containers Sequential: deque, list, vector Associative: map, multimap, multiset, set Adapters: priority_queue, queue, stack All STL containers use copy semantics.

6 6 The List Template The STL list template is a generic linked list class. We can use it to create a list of objects of any type: ints doubles Circles Dogs Cats

7 7 Frequently Used Methods size()Returns number of elements empty()True if list is empty front()Returns first item back()Returns last item push_back()Add item to end of list pop_back()Remove item from end of list push_front()Add item to beginning of list pop_front()Remove item from beginning of list sort()Sort using < operator Many more! See Drozdek pages 111 and 112.

8 8 Example Create a new project, List_Demo Add main.cpp #include using namespace std; int main(void) { cout << "This is List_Demo\n"; cout << "Normal termination\n"; cin.get(); return 0; }

9 9 Program Running

10 10 Using the list template #include... list int_list;

11 11 Using the list Template Add to main(): #include... list int_list; for (int i = 1; i <6 ; ++i) { cout << "Adding " << i << " to end of list\n"; int_list.push_back(i); } cout << "Here is the list:\n"; while (!int_list.empty()) { int next_item = int_list.front(); cout << next_item << endl; int_list.pop_front(); }

12 12 Program Running

13 13 Random Numbers #include using namespace std; int main(void) { list int_list; cout << "This is List_Demo\n"; for (int i = 1; i <6 ; ++i) { int val = rand() % 100; cout << "Adding " << val << " to end of list\n"; int_list.push_back(val); }

14 14 Program Running

15 15 Sorting the List int main(void) { list int_list; cout << "This is List_Demo\n"; for (int i = 1; i <6 ; ++i) { int val = rand() % 100; cout << "Adding " << val << " to end of list\n"; int_list.push_back(val); } cout << "Sorting the list\n"; int_list.sort(); cout << "Here is the list:\n";

16 16 Program Running

17 17 Sorting into Descending Order bool greater_than(int lhs, int rhs) { return lhs > rhs; } int main(void) {... cout << "Sorting the list into descending order\n"; int_list.sort(greater_than);

18 18 Program Running

19 19 Iterators The STL defines iterators as generalized pointers that permit user to iterate over all elements in a container. Permits the same code to be used with various kinds of containers. Use like a pointer.

20 20 Using a Iterator cout << "Iterating over the list\n"; list ::iterator i; i = int_list.begin(); while (i != int_list.end()) { int next_item = *i; cout << next_item << endl; ++i; }

21 21 Program Running

22 A List of Cats Last class we looked at putting Cats into our own Queue template. Let's use the same Cat class with the STL List template. Add class Cat to the project. Copy Cat.h and Cat.cpp from the Downloads area: http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_02_16_Queues_2/ http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_02_16_Queues_2/

23 A List of Cats Modify main to work with cats #include "Cats.h" int main(void) { list cat_list; get_cats(cat_list); cout << "Iterating over the list\n"; list ::iterator i; i = cat_list.begin(); while (i != cat_list.end()) { Cat next_item = *i; cout << next_item << endl; ++i; } cin.get(); return 0; }

24 get_cats Copy get_cats from Downloads area: http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_02_21_STL/get_cats.cpp.txt http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_02_21_STL/get_cats.cpp.txt void get_cats(list & cat_list) { Date dob = {12,1,2008}; Cat* c = new Cat("Fuzzy", dob, 4.5); cat_list.push_back(*c); delete c; Date dob2 = {2,1,2008}; c = new Cat("Fluffy", dob2, 8.4); cat_list.push_back(*c); delete c;

25 get_cats Date dob3 = {4, 4, 2002}; c = new Cat("Savanna", dob3, 12.0); cat_list.push_back(*c); delete c; Date dob4 = {5, 5, 1998}; c = new Cat("Raleigh", dob4, 12.8); cat_list.push_back(*c); delete c; Date dob5 = {10, 12, 2005}; c = new Cat("Tigger", dob5, 8.6); cat_list.push_back(*c); delete c; Date dob6 = {8, 1, 2000}; c = new Cat("Bucky", dob6, 14.9); cat_list.push_back(*c); delete c; }

26 Program in Action Let's delete the message from the destructor.

27 Program in Action

28 Sort the List The Cat class doesn't have a < operator. But we can supply one in main.cpp bool less_than(const Cat& lhs, const Cat& rhs) { return strcmp(lhs.Name(), rhs.Name()) < 0; } int main(void) { list cat_list; get_cats(cat_list); cout << "Sorting the list\n"; cat_list.sort(less_than); cout << "Iterating over the list\n"; list ::iterator i;

29 Program in Action

30 Sorting by Weight What if we want to sort the cats by weight rather than by name? Just provide a different function to the sort method: bool less_than(const Cat& lhs, const Cat& rhs) { //return strcmp(lhs.Name(), rhs.Name()) < 0; return lhs.Weight() < rhs.Weight(); }

31 Program in Action


Download ppt "1 The Standard Template Library Drozdek Section 3.7."

Similar presentations


Ads by Google