Templates & STL Instructor: 小黑
Templates Template serves as a class outline, from which specific classes are generated at compile time. One template can be used to generate many classes. template class temp_class { Name x,y; public: temp_class(); void fun1(); }; class temp_class { int x,y; public: temp_class(); void fun1(); }; class temp_class { char x,y; public: temp_class(); void fun1(); }; … temp_class a; temp_class b; …
Templates Two types of templates: Function Template Class Template Define Templates: template
Template’s Example void main() { X x1; //T1=int, T2=float X x2; //T1=char, T2= double* int i=3; fun( i); //T3=int } template class X ; //Class Template template void fun(T3 data) {} //Function Template //class and typename are the same
Partial Specialization template class X { public: X() { cout << “Construct”; } void display(T x) { cout << “Origin!”; } } ; template class X { public: X() { cout << “Construct”; } void display(int x) { cout << “Specialize!!”; } } ; X t1; X t2;
Function Specialization void X ::display(int x) { cout << “Integer!!”; } void X ::display(int x) { cout << “Character!!”; } template class X { public: X() { cout << “Construct”; } void display(T x) { cout << “Origin!”; } } ;
STL Container classes Sequences vector, list Associative Containers map, set Container adapters Stack, queue, priority_queue String String, rope bitset Operation/Utilities iterator algorithm
Vector #include using namespace std; int main () { vector v; // Template! for (int i=1; i<=10 ; i++) v.push_back(i); v.erase (v.begin()+5); v.erase (v.begin(),v.begin()+3); v.pop_back(); for (int i=0; i<v.size(); i++) cout << v[i] << " "; return 0; } Dynamic array of variables, struct or objects. Insert data at the end
Using iterator #include using namespace std; int main () { vector v1 (3,100); vector ::iterator it; it = v1.begin(); it = v1.insert (it,200); v1.insert (it,2,300); // "it" no longer valid it = v1.begin(); vector v2 (2,150); v1.insert( it+3, v2.begin(), v2.end()); int a1 [] = { 501,502,503 }; v1.insert (v1.begin(), a1, a1+3); for (it=v1.begin();it<v1.end(); it++) cout << *it << " "; cout << endl; return 0; }
Two and three dimensional vector #include using namespace std; int main () { vector > d2( 2,vector (3,0)); d2[0][0]=1, d2[0][1]=2, d2[0][2]=3; d2[1][0]=4, d2[1][1]=5, d2[1][2]=6; for (int i=0;i<(int)d2.size();i++){ for (int j=0; j<(int)d2[i].size();j++) cout << d2[i][j] << " "; cout << endl; } vector > > d3; d3.push_back(d2); cout << "Using iterator :" << endl; vector > >::iterator it1; vector >::iterator it2; vector ::iterator it3; for (it1=d3.begin();it1!=d3.end();it1++) for (it2=(*it1).begin(); it2!=(*it1).end();it2++) for (it3=(*it2).begin(); it3!=(*it2).end();it3++) cout << *it3 << " "; return 0; } Using iterator :
Map Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value. #include using namespace std; int main () { map mymap; mymap['a']="an element"; mymap['b']="another element"; mymap['c']=mymap['b']; cout << “'a' is " << mymap['a'] << endl; cout << “'b' is " << mymap['b'] << endl; cout << “'c' is " << mymap['c'] << endl; cout << “'d' is " << mymap['d'] << endl; cout << "mymap now contains " << (int)mymap.size() << " elements." << endl; return 0; } 'a' is an element 'b' is another element 'c' is another element 'd' is mymap now contains 4 elements.
Algorithm The header defines a collection of functions especially designed to be used on ranges of elements. We can accesses through iterators or pointers, such as an array or an instance of some of the STL containers. for_each( iterator, iterator, *func) find( iterator, iterator, T) find_if( iterator, iterator, *func) count( iterator, iterator, T) find(…), find_if(…) replace (…), replace_if(…) sort(…) binary_search(…) search(…)
Sort example #include using namespace std; bool myfunction (int i,int j) { return (i<j); } struct myclass { bool operator() (int i,int j) { return (i>j); } } myobject; int main () { int myints[]={32,71,12,45,26,80,53,33}; vector v(myints, myints+8); vector ::iterator it; //using default comparison (operator <): sort (v.begin(), v.begin()+4); //( ) // using function as comp sort (v.begin()+4, v.end(), myfunction); // ( ) // using object as comp sort (v.begin(), v.end(), myobject); //( ) cout << "sort:"; for (it=v.begin(); it!=v.end(); it++) cout << " " << *it; cout << endl; return 0; } sort: template void sort ( Iter first, Iter last );
Search example #include using namespace std; bool myfunction (int i,int j) { return (i==j); } int main () { vector v; vector ::iterator it; for ( int i=0 ; i<10 ; i++ ) v.push_pack(i*10); // using default comparison: int match1[] = {40,50,60,70}; it = search (v1.begin(), v1.end(), match1, match1+4); if (it!=v.end()) cout << “match1 found at position " << int(it-v.begin()) << endl; else cout << "match not found" << endl; // using predicate comparison: int match2[] = {20,30,50}; it = search (v.begin(), v.end(), match2, match2+3, myfunction); if (it!=v.end()) cout << "match2 found at position " << int(it-v.begin()) << endl; else cout << "match2 not found" << endl; return 0; } match1 found at position 3 match2 not found
Today’s practice Write a function called plus that uses function template. The function have two parameters and return the result of adding. Use algorithm ” for_each” to output all data in the output vector. You can check the usage from the web page:
Today’s practice // YOUR CODE HERE int main() { int i = plus( 6, 2); double d = plus(1.67,8.2); cout << i << endl << d << endl; string s1= plus ("he", "llo"); string s2= plus ( s1, " again"); string s3= plus ( "b", "ye!"); vector output; output.push_back(s1); output.push_back(s2); output.push_back(s3); // YOUR CODE HERE // for_each(…) return 0; } You need to fill these vacancies.
Reference