Professional Documents
Culture Documents
CSCI 207: Fundamentals of Data Structures & Algorithms: Review of Object Oriented Programming Basics
CSCI 207: Fundamentals of Data Structures & Algorithms: Review of Object Oriented Programming Basics
CSCI 207: Fundamentals of Data Structures & Algorithms: Review of Object Oriented Programming Basics
Algorithms
Lecture 3
1
Templates
Templates are C++ mechanism for implementing both template functions and
generic classes. The C++ template function uses the template mechanism to create
a function whose parameter types are not fixed with the definition of the function.
They are instantiated at the function call.
The template mechanism instantiates the parameter, in this case the class T,
when the class of function is called. For example, on encountering the call max(3,4),
the compiler instantiates T with type integer; explicitly call max<int>(3,4)
Templates are type safe which means the compiler will validate types while
compiling, and throw an error if you try to assign the wrong type to a variable.
2
Generic Classes
"A generic class is a class that serves as a template for other classes, in which the
template may be parameterised by other classes, objects and/or operations. A
generic class must be instantiated (its parameters filled in) before objects of that
class can be created”. Generic classes are typically used as container classes.
class intClass {
int storage[50];
..................
};
3
Generic Classes
However, in this way, we limit the usability of this class to integers only; if we
need a class that performs the same operations as intClass except that it operates
on float numbers, then a new declaration is needed, such as
class floatClass {
float storage[50];
..................
};
4
Generic Classes
It is much better to declare a generic class and decide what type of items the
object is referring to only when defining (creating or instantiating) the object.
Fortunately, C++ allows us to declare a class in this way, and the declaration for
the example is
template<class genType>
class genClass {
genType storage[50];
.................
};
genClass<int> intObject;
genClass<float> floatObject;
This generic class becomes a basis for generating two new classes, genClass
of int and genClass of float, and then these two classes are used to create two
objects: intObject and floatObject. 5
Generic Classes
We can go even further than that by not committing ourselves to 50 cells in
storage and by delaying that decision until the object definition stage. But just in
case, we may leave a default value so that the class declaration is now:
6
The Standard Template Library (STL)
Algorithms are frequently used functions that can be applied to different data
structures. The application is mediated through the iterators that determine which
algorithms can be applied to which types of objects.
The STL relieves programmers from writing their own implementations of various
classes and functions. Instead, they can use pre-packaged generic (general)
implementations accommodated to the problem at hand.
7
Containers
A container is a data structure that holds some objects that are usually of the
same type. Different types of containers organize the objects within them
differently.
The STL containers are implemented as template classes that include member
functions specifying what operations can be performed on the elements stored
in the data structure specified by the container or on the data structure (container)
itself.
8
Containers
The member functions common to all containers include the default constructor,
copy constructor, destructor, empty(), max_size(), size(), swap(), operator=, as
well as other overloaded relational operator functions for some containers.
Elements stored in containers can be of any type (int, float, CStudent, .etc.), and
they have to supply at least a default constructor, a destructor, and an assignment
operator.
Also, a copy constructor and the function operator = should be provided if data
members are pointers (why?).
9
Iterators
However, all iterators do not have similar functionality as that of pointers. That is
different containers support different iterators.
10
Iterators
No iterators are supported for the stack, queue, and priority_queue containers.
Iterator operations for classes list, map, multimap, set, and multiset are as
follows (i1 and i2 are iterators, n is a number):
11
Algorithms
The STL provides some 70 generic functions, called algorithms, that can be
applied to the STL containers and to arrays.
These algorithms are implementing operations that are very frequently used in
most programs, such as locating an element in a container, inserting an element into
a sequence of elements, removing an element from a sequence, modifying
elements, comparing elements, and so on.
Almost all STL algorithms use iterators to indicate the range of elements
on which they operate. For example,
i3 = find(i1, i2, el);
returns an iterator indicating the position of element el in the range i1 up to, but
not including i2.
counts with the algorithm count_if() the elements in the range indicated by
iterators i1 and i2 for which a one-argument user-defined Boolean function
oddNum() returns true.
12
Algorithms
Fill and Generate:
13
Vectors in the Standard Template Library
The simplest STL container is the vector, which is a data structure with
contiguous blocks of memory just like an array.
A vector is thus a flexible array; that is, an array whose size can be dynamically
changed.
14
Thank You
Questions?
15