0% found this document useful (0 votes)
10 views

Module V - STL

The document discusses the Standard Template Library (STL) in C++. It introduces the core components of STL - containers, algorithms, and iterators. Containers like vector, deque, list hold collections of objects. Algorithms manipulate container elements through iterators. Common containers include vectors for dynamic arrays, maps for key-value pairs. Iterators provide access to container elements similar to pointers. STL optimizes for performance through templates and container-algorithm independence.

Uploaded by

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

Module V - STL

The document discusses the Standard Template Library (STL) in C++. It introduces the core components of STL - containers, algorithms, and iterators. Containers like vector, deque, list hold collections of objects. Algorithms manipulate container elements through iterators. Common containers include vectors for dynamic arrays, maps for key-value pairs. Iterators provide access to container elements similar to pointers. STL optimizes for performance through templates and container-algorithm independence.

Uploaded by

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

Object Oriented Programming

using C++
Course Code: ES203
MODULE V-STL
Ms. Garima Srivastava
Asstt. Professor
Dept Of CSE/IT ASET
AUUP Lucknow
Standard Template Library
• The C++ Standard Library now offers a suite of collection
classes called the Standard Template Library (STL).
• The STL classes are designed to hold collections of
objects, including built-in objects such as characters and
more complex (and dramatically larger) user defined
objects.
• Most importantly, the STL code has been optimized,
debugged, and tested so that you don't have to do this
work yourself.

2
• At the core of the standard template library are three
foundational items: containers, algorithms, and iterators.
• These items work in conjunction with one another to
provide off-the-shelf solutions to a variety of programming
problems.

3
• STL algorithms are independent of containers, which
significantly reduces the complexity of the library.
• The STL achieves its results through the use of
templates. This approach provides compile-time
polymorphism that is often more efficient than traditional
run-time polymorphism.
• Modern C++ compilers are tuned to minimize any
abstraction penalty arising from heavy use of the
• STL

4
Containers
• Containers are objects that hold other objects, and there
are several different types.
• For example, the vector class defines a dynamic array,
deque creates a double-ended queue, and list provides a
linear list.
• These containers are called sequence containers
because in STL terminology, a sequence is a linear list.
• In addition to the basic containers.

5
• The STL also defines associative containers, which allow
efficient retrieval of values based on keys.
• For example, a map provides access to values with
unique keys.
• Thus, a map stores a key/value pair and allows a value to
be retrieved given its key.
• Each container class defines a set of functions that may
be applied to the container.

6
• For example, a list container includes functions that
insert, delete, and merge elements. A stack includes
functions that push and pop values.

7
Algorithms
• Algorithms act on containers.
• They provide the means by which you will manipulate the
contents of containers.
• Their capabilities include initialization, sorting, searching,
and transforming the contents of containers.
• Many algorithms operate on a range of elements within a
container.

8
Iterators
• Iterators are objects that are, more or less, pointers.
• They give you the ability to cycle through the contents of
a container in much the same way that you would use a
pointer to cycle through an array.
• There are five types of iterators:
• Random Access -Store and retrieve values. Elements
may be accessed randomly.
• Bidirectional Store and retrieve values. Forward and
backward moving.
9
• Forward -Store and retrieve values. Forward moving only.
• Input- Retrieve, but not store values. Forward moving
only.
• Output- Store, but not retrieve values. Forward moving
only.
• In general, an iterator that has greater access capabilities
can be used in place of one that has lesser capabilities.
For example, a forward iterator can be used in place of an
input iterator. Iterators are handled just like pointers. You
can increment and decrement them.
10
• You can apply the * operator to them.
• Iterators are declared using the iterator type defined by
the various containers.
• The STL also supports reverse iterators.
• Reverse iterators are either bidirectional or random-
access iterators that move through a sequence in the
reverse direction.
• Thus, if a reverse iterator points to the end of a sequence,
incrementing that iterator will cause it to point to one
element before the end.
11
Vectors
• Perhaps the most general-purpose of the containers is vector.
• The vector class supports a dynamic array.
• This is an array that can grow as needed.
• As you know, in C++ the size of an array is fixed at compile time.
• While this is by far the most efficient way to implement arrays, it is
also the most restrictive because the size of the array cannot be
adjusted at run time to accommodate changing program
conditions.
• A vector solves this problem by allocating memory as needed.
Although a vector is dynamic, you can still use the standard array
subscript notation to access its elements.
12
13
14
15
16

You might also like