the standard template library
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/1.jpg)
1
The Standard Template Library
Chapter 16
![Page 2: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/2.jpg)
Announcement
No class on Friday, Feb. 18
Engineering Expo
Participate!
2
![Page 3: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/3.jpg)
3
Objectives
You will be able to use the Standard Template Library list class in C++ programs.
![Page 4: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/4.jpg)
4
C++ Templates
We often need essentially identical classes for use with different kinds of objects.
Examples: Lists Stacks Queues
Compare to arrays
![Page 5: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/5.jpg)
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.
![Page 6: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/6.jpg)
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.
![Page 7: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/7.jpg)
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
![Page 8: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/8.jpg)
8
The Standard Template Library
Big Subject Brief introduction today.
We will use one template as an example:
The list template
![Page 9: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/9.jpg)
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
![Page 10: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/10.jpg)
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.
![Page 11: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/11.jpg)
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
![Page 12: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/12.jpg)
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!
![Page 13: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/13.jpg)
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;
}
![Page 14: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/14.jpg)
14
Program Running
![Page 15: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/15.jpg)
15
Using the list template
#include <list>
...
list<int> int_list;
![Page 16: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/16.jpg)
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();
}
![Page 17: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/17.jpg)
17
Program Running
![Page 18: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/18.jpg)
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);
}
![Page 19: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/19.jpg)
19
Program Running
![Page 20: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/20.jpg)
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";
![Page 21: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/21.jpg)
21
Program Running
![Page 22: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/22.jpg)
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);
![Page 23: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/23.jpg)
23
Program Running
![Page 24: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/24.jpg)
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.
![Page 25: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/25.jpg)
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;
}
![Page 26: The Standard Template Library](https://reader035.vdocuments.net/reader035/viewer/2022070400/56813554550346895d9cb745/html5/thumbnails/26.jpg)
26
Program Running
End of Presentation