Professor: Munehiro Fukuda - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Professor: Munehiro Fukuda

Description:

You cannot compile SortedList.cpp independently. SortedList.cpp should be treated as if it were a part of a header file. ... Compile SortedListDriver.cpp only. ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 17
Provided by: munehir
Category:

less

Transcript and Presenter's Notes

Title: Professor: Munehiro Fukuda


1
CSS342 Template
  • Professor Munehiro Fukuda

2
Todays Topics
  • Function templates
  • Explained using a bubble sort example
  • Class templates
  • Explained using an insertion sort example
  • Template compilation
  • STL

3
Function Templates
Function Templates
  1. If you focus on just integers
  1. If you use doubles instead

typedef double Object Object myMin( Object a,
Object b ) return ( a lt b ) ? a b int
main( ) cout ltlt myMin( 7.7, 5.5 ) ltlt endl
int myMin( int a, int b ) return ( a lt b ) ?
a b int main( ) cout ltlt myMin( 7, 5 )
ltlt endl
  1. If you want to use both integers and doubles

template ltclass Objectgt Object myMin( Object a,
Object b ) return ( a lt b ) ? a b int
main( ) cout ltlt myMin( 7, 5 ) ltlt endl
cout ltlt myMin( 7.7, 5.5 ) ltlt endl
4
Function Template Example 1Swap
Function Templates
int main( ) int x 5, y 7 double a 2,
b 4 string p "abc", q "xyz"
vectorltintgt v(5, 5), w(7, 7) mySwap( x, y )
cout ltlt x ltlt " " ltlt y ltlt endl mySwap( a, b
) cout ltlt a ltlt " " ltlt b ltlt endl mySwap( p,
q ) cout ltlt p ltlt " " ltlt q ltlt endl mySwap(
v, w ) for ( int i 0 i lt v.size( ) i )
cout ltlt vi for ( int i 0 i lt w.size( )
i ) cout ltlt wi cout ltlt endl
include ltiostreamgt include ltstringgt include
ltvectorgt using namespace std template ltclass
Objectgt void mySwap( Object lhs, Object rhs
) Object tmp lhs lhs rhs rhs
tmp
5
Function Templates
Function Template Example 2Bubble Sort
Pass 1
Pass 2
Pass 3
29
10
14
13
37
29
10
14
13
37
13
10
14
29
37
29
10
14
13
37
29
10
14
13
37
13
10
14
29
37
29
10
14
13
37
29
10
14
13
37
29
10
13
14
37
29
10
14
13
37
29
10
14
13
37
Pass 4
29
10
14
13
37
14
10
13
29
37
29
10
13
14
37
6
Function Templates
Function Template Example 2Bubble Sort
(Continued)
include ltiostreamgt include ltvectorgt include
ltstringgt using namespace std template ltclass
Objectgt void bubbleSort( vectorltObjectgt a )
bool swapOccurred true // true when swaps
occur for ( int pass 1 ( pass lt a.size( ) )
swapOccurred pass ) swapOccurred
false // swaps have not occurred at the
beginning for ( int i 0 i lt a.size( ) -
pass i ) // a bubble(i) goes from 0 to
size - pass if ( ai gt ai 1 ) swap(
ai, ai 1 ) swapOccurred true // a swap
has occured
7
Function Templates
Function Template Example 2Bubble Sort
(Continued)
int main( ) vectorltintgt intArray // The
integer array int x // An
integer to read cout ltlt "Enter positive
integers to sort ltlt endl while ( true )
cin gtgt x if ( x gt 0 )
intArray.push_back( x ) else break
bubbleSort( intArray )
vectorltstringgt strArray // The string array
string s // a string to read
cout ltlt "Enter strings to sort ltlt endl
while( cin gtgt s ) strArray.push_back( s )
bubbleSort( strArray ) cout ltlt "Sorted
strings are " ltlt endl for ( int i 0 i lt
strArray.size( ) i ) cout ltlt strArrayi
ltlt " " cout ltlt endl return 0
8
Class Templates
Class Templates
  • Private data members and a methods return value
    may be any class.
  • Consider a sorted list whose item may be a string
    or an integer

class SortedList public void insert( const
string_or_int item ) const SortedList
operator( const SortedList rhs ) void
print( ) const private string_or_int array
void insertionSort( ) int main( )
SortedListltstringgt strList strList.insert(
abc ) SortedListltintgt intList
intList.insert( 5 )
Sorted
a
b
c
d
f
a
b
c
d
e
insert( )
Sorted
1
2
3
4
5
1
2
3
4
5
insert( )
9
How to Define the Template Header in .h (Sorted
List Example)
Class Templates
ifndef _SORTEDLIST_H_ define _SORTEDLIST_H_ in
clude ltiostreamgt include ltvectorgt using
namespace std templateltclass Objectgt class
SortedList public void insert( const Object
item ) const SortedListltObjectgt operator(
const SortedListltObjectgt rhs ) void print( )
const private vectorltObjectgt array void
insertionSort( ) include "SortedList.cpp
// discuss about this technique endif
At compilation time, Object will be replaced with
int or string
10
How to Code the Template Implementation in .cpp
(Sorted List Example)
Class Templates
ifndef _SORTEDLIST_CPP_ define
_SORTEDLIST_CPP_ template ltclass Objectgt void
SortedListltObjectgtinsert( const Object item )
array.push_back( item ) // insert a new
item insertionSort( ) // sort
items template ltclass Objectgt const
SortedListltObjectgt SortedListltObjectgt operator
( const SortedListltObjectgt rhs ) //
concatenate two lists SortedListltObjectgt list
new SortedListltObjectgt list-gtarray
array for ( int i 0 i lt rhs.array.size( )
i ) list-gtarray.push_back( rhs.arrayi
) return list
At compilation time, Object will be replaced with
int or string
11
How to Code the Template Implementation in .cpp
(Sorted List Example)
Class Templates
template ltclass Objectgt void SortedListltObjectgtp
rint( ) const // print out all sorted item
for ( int i 0 i lt array.size( ) i )
cout ltlt arrayi ltlt " " cout ltlt
endl template ltclass Objectgt void
SortedListltObjectgtinsertionSort( ) for (
int unsorted 1 unsorted lt array.size( )
unsorted ) // Assume the 0th item is
sorted. Unsorted items start from the 1st item
Object unsortedTop arrayunsorted // Copy
the top of unsorted group int i for ( i
unsorted - 1 ( i gt 0 ) (arrayi gt
unsortedTop ) --i ) // Upon a successful
comparison, shift arrayi to the right
arrayi 1 arrayi // insert the
unsorted top into the sorted group arrayi
1 unsortedTop endif
See the next slide for graphical explanation
unsortedTop
13
insert
copy
compare
unsorted
13
11
25
20
37
29
14
10
8
3
2
loc loc1
loc loc1
shift
loc loc1
37
29
14
12
Insertion Sort
Class Templates
Sorted
Unsorted
29
10
14
13
37
Copy 10
Shift 29
29
29
14
13
37
29
10
14
13
37
Insert 10, copy 14
unsortedTop
29
10
29
13
37
Shift 29
14
10
29
13
37
Insert 14 copy 37
14
10
29
13
37
Shift nothing
14
10
29
13
37
Insert 37 Copy 13
14
10
14
37
29
Shift 37, 29 and 14.
13
10
14
37
29
Insert 13
13
Tips for Template Compilation
Template Compilation
  • You cannot compile SortedList.cpp independently.
  • SortedList.cpp should be treated as if it were a
    part of a header file.
  • SortedList.h at the end of this header file,
    add
  • include SortedList.cpp
  • SortedList.cpp at the beginning of this cpp
    file, do Not add
  • include SortedList.h
  • SortedListDriver.cpp
  • Compile SortedListDriver.cpp only.
  • In Eclipse, it is a bit tricky to exclude
    SortedList.cpp from compilation. To make it
    simple, we could name it as SortedList.cpp.h.

14
Iterators
Standard Template Library
  • A software design pattern that abstracts the
    process of scanning through a collection of
    elements
  • The index of vector i can be considered as a
    primitive iterator which is however not enough
  • The iterator should include
  • The current position (like vectori)
  • The retrieval of the current item (like
    vectori)
  • The way of stepping to the next position (forward
    or backward)

15
Vector and Iterators
Standard Template Library
  • Vector has forward and reverse iterators

include ltiostreamgt include ltvectorgt using
namespace std int main( ) vectorltintgt
array for ( int i 0 i lt 10 i )
array.push_back( i ) vectorltintgtiterator f
// forward iterator for ( f array.begin( )
f ! array.end( ) f ) cout ltlt f ltlt
endl vectorltintgtreverse_iterator b //
reverse iterator for ( b array.rbegin( ) b
! array.rend( ) b ) cout ltlt b ltlt endl
1
0
2
3
4
5
6
7
8
9
16
Standard Template Library
Standard Template Library
  • Standard Template Library
  • Provides typical templates such as vector, list,
    stack, and queue.
  • At least, we have studied vector already.
  • Containers
  • Elements stored in STL template class
  • Iterators
  • A pointer to a container in operation.
Write a Comment
User Comments (0)
About PowerShow.com