0% found this document useful (0 votes)
4 views9 pages

C++ Templates and STL Overview

Uploaded by

s210859
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)
4 views9 pages

C++ Templates and STL Overview

Uploaded by

s210859
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

UNIT-5:

Classes Templates& STL - Function and Class templates and using


STL like containers,
algorithms
• A template is a simple yet very powerful tool in C+
+. The simple idea is to pass data type as a parameter
so that we don’t need to write the same code for
different data types. For example, a software
company may need to sort() for different data types.
Rather than writing and maintaining multiple codes,
we can write one sort() and pass data type as a
parameter
• There are two ways we can implement
templates:
• Function Templates
• Class Templates
Class Template:-
• A class template starts with the
keyword template followed by template parameter(s)
inside <> which is followed by the class declaration.

• Syntax:-

• template <class T>


• class className
•{
• private:
• T var; ... .. ...
• public:
• T functionName(T arg); ... .. ...
• };
How Creating a Class Template Object

• Once we've declared and defined a class


template, we can create its objects in
other classes or functions (such as
the main() function) with the following
syntax.

• className<dataType> classObject;
Components of STL
• STL has major components:-
• Containers
• Algorithms

• Containers:-
• A container is an object that stores a collection of
objects of a specific type. For example, if we need to
store a list of names, we can use a vector

• If you are dealing with many elements, then you


need to use some sort of container. The container
can be described as objects that are used to store
the collection of data. It helps in recreating and
implementing complex data structures efficiently.
• array: Static contiguous array (class
template)
• vector: Dynamic contiguous array (class
template)
• deque: Double-ended queue (class template)
• forward_list: Singly-linked list (class
template)
• list: Doubly-linked list (class template)
Algorithms:-
• In STL, different types of algorithms can be
implemented with the help of iterators.
Algorithms can be defined as functions
applied to the containers and provide
operation for the content of the container.
for example : sort(), swap(), min(), max() etc.
• Types of algorithms:
• Modifying algorithms
• Non-modifying algorithms
• Sorting algorithms
• Searching algorithms
• Numeric algorithms

You might also like