Newest Viewed Downloaded

function template (cont.)int A[100]; float B[1000]; … Insertionsort(A, 100); Insertionsort(B,1000); …

Templates

code reuse inheritance template classes template classes a class that is not data-type specific eg. a class of Array of any type intArray, charArray, stringArray

template

格式: template class classname { …. }; e.g., class Array of any type

template

template class Array { public: T& operator[ ] (int); Array(int); ~Array( ); int get_size( ) const { return size; } private: T* a; int size; T dummy_val; };

template

template T& Array::operator[ ](int i) { if (i < 0 || i >= size) { cerr << “index “ << I << “out of bounds”; return dummy_val; } return a[i]; } template const T& Array::operator[ ](int i) const { if (i < 0 || i >= size) { cerr << “index “ << I << “out of bounds”; return dummy_val; } return a[i]; }

template

template Array::Array(int s) { a = new T[size =s]; } …

template

int main( ) { Array a(100); // an integer array Array b(200); // a char array Array c(300); // a float array a[10] = 30; b[15] = ‘s’; c[20] = 35.99; … }

template

more than one class parameters e.g. template class Sample { public: T2 m(T3 p) { … } private: T1 x; … };

template

function-style parameters e.g. template class Array { public: Array( ); T& operator[ ] (int); … private: T* a; int size; T dummy_val; }; template Array::Array( ) { a = new T[size = s]; }

template

void main( ) { Array a1; Array a2; a1[5] = 20.9; a2[30] = ‘c’; … }

template

Example: A template Stack class p. 353 int main( ) { Stack s1; Stack s2; … }

template

function templates (top-level functions) e.g. template T min(const T &a, const T &b) { if (a < b) return a; else return b; }

function template (cont.)

int main ( ) { int i=10; j=20; cout << min(i, j) <

function template (example)

template void Insertionsort(T a[ ], int n) { for (int i=1; i0) && (v

function template (cont.)

int A[100]; float B[1000]; … Insertionsort(A, 100); Insertionsort(B,1000); …

Standard Template Library(STL)

STL a part of the standard C++ library provide C++ with data structures Three elements in STL Containers template classes Algorithms functions that process the contents of containers Iterators “Pointers” for accessing the container’s objects

standard template library

example: #include #include #include using namespace std; int main( ) { vector v; // container deque d; // container … sort(v.begin( ), v.end( )); // algorithm and iterators sort(d.begin( ), d.end( )); … }

standard template library

basic containers sequential list, vector, deque associative set, multiset, map, multimap

standard template library

Member functions for all containers Default constructor, copy constructor, destructor empty max_size, size = < <= > >= == != swap Functions for first-class containers begin, end rbegin, rend erase, clear, insert

standard template library

vector an array that grows and shrinks as needed e.g. vector d; // no need to specify size vector a(100); // initialize 100 elements to 0, still grow // dynamically more examples……

STL -- vector

#include #include using namespace std; int main( ) { int i; vector nums; nums.insert(nums.begin( ), -999); nums.insert(nums.begin( ), 14); nums.insert(nums.end( ), 57); for (i = 0; i < num.size( ); i++) cout << nums[ i ] << endl;

Showing 1 - 20 of 44 items Details

Name: 
chap7
Author: 
isu
Company: 
isu
Description: 
function template (cont.)int A[100]; float B[1000]; … Insertionsort(A, 100); Insertionsort(B,1000); …
Tags: 
int | template | include | cout | endl | begin | class | array
Created: 
3/6/2003 6:56:05 AM
Slides: 
44
Views: 
0
Downloads: 
0
Rating: 
0


> Comment



Share this presentation
|

Comments

Share this presentation:

|
Sitemap