1 / 26

The Standard Template Library

The Standard Template Library. Chapter 16. Announcement. No class on Friday, Feb. 18 Engineering Expo Participate!. Objectives. You will be able to use the Standard Template Library list class in C++ programs. C++ Templates.

Télécharger la présentation

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

  2. Announcement • No class on Friday, Feb. 18 • Engineering Expo • Participate!

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

  4. C++ Templates • We often need essentially identical classes for use with different kinds of objects. • Examples: • Lists • Stacks • Queues • Compare to arrays

  5. C++ Templates • The C++ template permits us to define classes with blanks to be filled in with specific class names at compile time. • Called the template parameter. • A template can have multiple parameters. • The template class cannot be instantiated directly. • It is a pattern from which classes can be created.

  6. C++ Templates • Specifying the parameter configures the template as a real class definition. • Filling in the blanks. • When we declare an object of a template class and supply its parameters, the compiler creates a real class. • A template with parameters can be used like a normal class. • Compare to #define.

  7. C++ Templates • You can define templates in a C++ program just as you can define normal classes. • Not the subject of this presentation. • We will only be using an existing template. • There is a large library of predefined templates available with any C++ compiler. • The C++ Standard Template Library

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

  9. 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

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

  11. 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

  12. 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!

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

  14. Program Running

  15. Using the list template #include <list> ... list<int> int_list;

  16. Using the list Template Add to main(): #include <list> ... list<int> 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(); }

  17. Program Running

  18. Random Numbers #include <iostream> #include <list> #include <cstdlib> using namespace std; int main(void) { list<int> 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); }

  19. Program Running

  20. Sorting the List int main(void) { list<int> 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";

  21. Program Running

  22. 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);

  23. Program Running

  24. 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.

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

  26. Program Running End of Presentation

More Related