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

The Standard Template Library PowerPoint PPT Presentation


  • 107 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

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

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

  • Examples:

    • Lists

    • Stacks

    • Queues

  • Compare to arrays


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++ 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++ 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 Library

  • Big Subject

    • Brief introduction today.

  • We will use one template as an example:

    • The list template


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

Sequential:deque, list, vector

Associative:map, multimap,

multiset, set

Adapters:priority_queue,

queue, stack

All STL containers use copy semantics.


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

  • 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

  • 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


Using the list template

#include <list>

...

list<int> int_list;


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 Running


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 Running


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 Running


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 Running


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

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 Running

End of Presentation


  • Login