The standard template library
This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

The Standard Template Library PowerPoint PPT Presentation


  • 103 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


The standard template library

The Standard Template Library

Chapter 16


Announcement

Announcement

  • No class on Friday, Feb. 18

  • Engineering Expo

  • Participate!


Objectives

Objectives

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


C templates

C++ Templates

  • We often need essentially identical classes for use with different kinds of objects.

  • Examples:

    • Lists

    • Stacks

    • Queues

  • Compare to arrays


C templates1

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.


C templates2

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.


C templates3

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


The standard template library1

The Standard Template Library

  • Big Subject

    • Brief introduction today.

  • We will use one template as an example:

    • The list template


Stl components

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


The ten stl containers

The Ten STL Containers

Sequential:deque, list, vector

Associative:map, multimap,

multiset, set

Adapters:priority_queue,

queue, stack

All STL containers use copy semantics.


The list template

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


Frequently used methods

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!


Example

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;

    }


Program running

Program Running


Using the list template

Using the list template

#include <list>

...

list<int> int_list;


Using the list template1

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();

}


Program running1

Program Running


Random numbers

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

}


Program running2

Program Running


Sorting the list

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";


Program running3

Program Running


Sorting into descending order

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


Program running4

Program Running


Iterators

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.


Using a iterator

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;

}


Program running5

Program Running

End of Presentation


  • Login