the standard template library

26
1 The Standard Template Library Chapter 16

Upload: prudence-bender

Post on 03-Jan-2016

42 views

Category:

Documents


3 download

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 Presentation

TRANSCRIPT

Page 1: The Standard Template Library

1

The Standard Template Library

Chapter 16

Page 2: The Standard Template Library

Announcement

No class on Friday, Feb. 18

Engineering Expo

Participate!

2

Page 3: The Standard Template Library

3

Objectives

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

Page 4: The Standard Template Library

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

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

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

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

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

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

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

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

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

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

14

Program Running

Page 15: The Standard Template Library

15

Using the list template

#include <list>

...

list<int> int_list;

Page 16: The Standard Template Library

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

17

Program Running

Page 18: The Standard Template Library

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

19

Program Running

Page 20: The Standard Template Library

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

21

Program Running

Page 22: The Standard Template Library

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

23

Program Running

Page 24: The Standard Template Library

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

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

26

Program Running

End of Presentation