dis5

dis5 - CS32
Discussion
 Sec.on
1B
 Week
6
...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS32
Discussion
 Sec.on
1B
 Week
6
 TA:
Brian
Choi
 Template
Classes 
 class Pair { public: Pair(); Pair(int firstValue, int secondValue); void setFirst(int newValue); void setSecond(int newValue); int getFirst() const; int getSecond() const; private: int m_first; int m_second; }; •  This
class
 works
only
 with
integers.
 •  Can
we
make
a
 “generic”
Pair
 class?
(Note
 that
typedef
 does
not
do
 the
job
for
us.)
 Template
Classes 
 template<typename T> class Pair { public: Pair(); Pair(T firstValue, T secondValue); void setFirst(T newValue); void setSecond(T newValue); T getFirst() const; T getSecond() const; private: T m_first; T m_second; }; •  Here
we
go.
 Pair<int> p1; Pair<char> p2; Template
Classes 
 template<typename T, U> class Pair { public: Pair(); Pair(T firstValue, U secondValue); void setFirst(T newValue); void setSecond(U newValue); T getFirst() const; U getSecond() const; private: T m_first; U m_second; }; •  Here
we
go.
 Pair<int, int> p1; Pair<string, int> p2; Template
Classes 
 template<typename T> void Pair<T>::setFirst(T newValue) { m_first = newValue; } •  Member
 func.ons
 should
be
 edited
as
well.
 Template
Specializa.on 
 template<> class Pair<char> { public: Pair(); Pair(char firstValue, char secondValue); void setFirst(char newValue); void setSecond(char newValue); char getFirst() const; char getSecond() const; void uppercase(); private: char m_first; char m_second; }; •  Here
we
go.
 Pair<char> p1; Pair<int> p2; p1.uppercase(); (O) P2.uppercase(); (X) Template
Func.ons 
 template<typename T> void swap(T &x, T &y) { T temp = x; x = y; y = temp; } •  Same
syntax
 with
func.ons,
 yet
the
only
 difference
 being:
 int x = 2, y = 3; swap(x, y); char j = ‘c’, k = ‘m’; Swap(j, k); 
 STL 
 •  Standard
Template
Library.
 –  vector (array)
 –  set –  list (doubly
linked
list)
 –  map –  stack –  queue STL 
 •  You
don’t
have
to
memorize
names
of
 member
func.ons
for
each
–
you
can
just
look
 things
up
when
you
need
to.
 
e.g.
hYp://www.cplusplus.com/reference/stl/
 •  But
do
remember
what
data
structure
each
 container
implements.
 STL 
 •  A
few
common
func.ons
(not
for
stack/queue):
 
.size() .empty() .insert() .erase() set<int> s; s.insert(5); s.size(); // 1 s.empty(); // false ... STL 
 •  Suppose
I
want
to
iterate
through
elements
in
a
 container:
 •  For
an
array,
you
would
do:
 int arr[100]; ... for (int i = 0; i < 100; i++) { cout << arr[i] << endl; } •  But
how
do
we
do
this
for
a
list
or
a
set?
 STL
Iterator 
 •  “abstract”
way
of
traversing
through
elements •  structure<data type>::iterator
‐‐
pointer
to
an
 element
in
a
container
 •  .begin()
gives
you
the
“first”
element
in
the
container
 •  .end()
indicates
that
the
itera.on
is
complete
 list<int> l; for (list<int>::iterator it = l.begin(); it != l.end(); it++) { cout << *it << “ “; // Note that ‘*’!! } STL
Iterator 
 •  Use
const_iterator
when
the
container
is
 constant! void func(const list<int> &l) { for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) { cout << *it << “ “; } } STL
Iterator 
 •  If
you
need
to
iterate
in
the
reverse
direc.on,
 you
can
op.onally
use
rbegin()
and
rend(): void func(const list<int> &l) { for (list<int>::const_iterator it = l.rbegin(); it != l.rend(); it++) { cout << *it; // Note that ‘*’!! } } Quick
Note
on
erase() 
 •  Suppose
you’re
givena
structure
and
would
 like
to
remove
all
elements
that
sa.sfy
a
 certain
condi.on: for (list<int>::iterator it = l.begin(); it != l.end(); it++) { if (*it == 10) { l.erase(it); // remove the element pointed by it } } •  What
is
the
problem
here?
 Quick
Note
on
erase() 
 •  Suppose
you’re
given
a
structure
and
would
 like
to
remove
all
elements
that
sa.sfy
a
 certain
condi.on: for (list<int>::iterator it = l.begin(); it != l.end();) { if (*it == 10) { it = l.erase(it); // remove the element pointed by it } else it++; } •  erase()
returns
an
iterator
for
the
next
element.
 Use
STL
containers
for
Project
3 
 •  Using
STL
containers
is
not
a
requirement
for
 Project
3,
but
using
them
will
reduce
a
lot
of
 work
for
you.
 •  e.g.,
 
list<Actor *> m_actors; ...
View Full Document

Ask a homework question - tutors are online