0% found this document useful (0 votes)
9 views28 pages

Introduction

The document provides an overview of the Standard Template Library (STL) in C++, including its history, components, and usage of templates. It highlights the importance of STL in providing reusable components for data structures and algorithms, as well as the syntax and functionality of template classes. Key concepts such as containers, iterators, and algorithms are also discussed, along with examples of their implementation.

Uploaded by

mdadilhossen0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views28 pages

Introduction

The document provides an overview of the Standard Template Library (STL) in C++, including its history, components, and usage of templates. It highlights the importance of STL in providing reusable components for data structures and algorithms, as well as the syntax and functionality of template classes. Key concepts such as containers, iterators, and algorithms are also discussed, along with examples of their implementation.

Uploaded by

mdadilhossen0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

STL

Standard Template Library



Good reference book:
– The C++ Standard Library

A Tutorial and Reference

by Nicolai M. Josuttis

1999 – Addison Wesley Longman, Inc.

ISBN: 0201379260

My presentation will come from both
books plus a small amount from
other references.
– www.cs.rpi.edu/projects/STL/stl/stl.html
History

1989 – Start of standardization of C++

1997 – Finished, published Sept. 1998

750 pages, published by the International
Standards Organization (ISO).

Title: “Information Technology –
Programming Languages – C++”

“..it defines the exact contents and
behavior of C++, ...”

Part of the standard is a standard library.
History: C++ Standard
Library

core components for
– I/O
– strings,
– containers (data structures)
– algorithms (such as sort, search and
merge)
– support for numeric computation
– support for internationalization (such
as different character sets)
History: STL added

1994 – The STL was added to the
C++ standard library.

STL defines powerful, template-
based, reusable components that
implement many common data
structures and algorithms used to
process those data structures.

Makes heavy use of both function
and class templates.
Templates

Templates are functions or classes
that are written for one or more
types not yet specified.

When you use a template, you
pass the types as arguments,
explicitly or implicitly.
Template Example

template <class T>

inline const T& my_max (const
T& a,
const T& b)

{

if(a < b) return b;

else return a;

}
exmaple : details

First line defines T as an arbitrary data
type that is specified by the caller
when the caller calls the function. The
type is classified by class, although it
does not have to be a class. You can
use any data type as long as it
provides the operations that the
template uses.

inline is a hint to the compiler to put
this function in-line since it is so short.
example: details

const before the type reference
allows the function to use constant
reference parameters.

T& is the type of the function. (A
reference to the type T)

my_max is the name of the function
being defined as a template.

const T& a (and const T& b) define
two reference parameters whose
value cannot be changed.
example: details

The code for the function itself is
simply to return the maximum of
the two parameter values as the
value of the function.

Instantiated by its use (since it is a
function and not a class).

Use:
– int a(10), b(12);
– cout << my_max(a,b) << endl;

Output: 12
MyArray class template
– template <class T>
– class MyArray {
– public:
– MyArray(int = 10);
– ~MyArray( );
– void FillArray(T=2);
– void PrintArray( );
– private:
– T* ArrayPtr;
– int size;
– };
MyArray constructor
– template < class T>
– MyArray<T>::MyArray(int s)
– {
– if(s < 0) s = 10;
– size = s;
– ArrayPtr = new T[s];
– }
MyArray destructor
– template < class T >
– MyArray<T>::~MyArray( )
– {
– delete [ ] ArrayPtr;
– }
MyArray FillArray function
– template < class T >
– void MyArray<T>::FillArray(T value)
– {
– for(int i=0; i<size; i++)
– ArrayPtr[i]=value;
– }
MyArray PrintArray
function
– template < class T >
– void MyArray<T>::PrintArray( )
– {
– for(int i = 0; i<size; i++)
– cout << ArrayPtr[i] << “ “;
– cout << endl;
– }
test MyArray
– #include “MyArray.h”
– int main (void)
– {
– MyArray<int> fred(3);
– MyArray<double> suzie(5);
– MyArray<int> sam;

– fred.FillArray(-2); fred.PrintArray();
– suzie.FillArray(3.25);
suzie.PrintArray();
– sam.FillArray(); sam.PrintArray();
– }
test output
– -2 -2 -2
– 3.25 3.25 3.25 3.25 3.25
– 2 2 2 2 2 2 2 2 2 2
STL General Concepts

Namespace std

(covered before)
– std::cout << std::hex << 127 <<
std::endl;
– or
– using std::cout;
– using std::endl;
– cout << std::hex << 127 << endl;
– or
– using namespace std;
– cout << hex << 127 << endl;
STL General Concepts
cont.

Header files

In addition to the standard ones:
– #include <iostream>

There is normally a special include
for each use of a STL template.
– #include <string>
– #include <vector>
– #include <stack>

(more specifics as we describe STL
components)
STL
Components

Containers
– Manage collections of objects of a
certain kind.

Iterators
– Used to step through the elements of
collections of objects.
– Small but common interface for an
arbitrary container type.

Algorithms
– Used to process elements of collections
container iterator

iterator
algorithm container

container iterator
STL
Sequence Containers

Ordered Collections.

Every element has a certain position.

Position independent of the value of
the element.

Position depends on order and place
of insertion.
– Vector
– Deque
– List
STL
Sequence Containers
Vector

Deque

List
STL
Associative Containers

Sorted Collection.

Actual position of an element
depends on its value due to certain
sorting criterion.

Order of insertion doesn't matter.
– Set
– Multiset
– Map
– Multimap
STL
Associative Containers
Set/Multiset

Map/Multimap
STL Iterators
– An object that can “iterate” over
elements. May be all or part of a STL
container.
– An iterator represents a certain
position in a container.
– Operator *

Returns the element of the actual
position.
– Operator ++

Lets the iterator step forward to the next
element.

Most iterators also allow stepping
backwards by using operator - -
(cont.)

Operator == and !=

Returns whether two iterators represent
the same position.
– Operator =

Assigns an iterator (the position of the
element to which it refers)
Iterator related member
functions

begin( )
– returns an interator that represents
the beginning of the elements in the
container.

end( )
– returns an iterator that represents the
end of the elements in the container.
– The end position is behind the last
element.
begin ( ) end ( )

You might also like