Presentation is loading. Please wait.

Presentation is loading. Please wait.

Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.

Similar presentations


Presentation on theme: "Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template."— Presentation transcript:

1 Templates & STL Instructor: 小黑

2 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; …

3 Templates Two types of templates:  Function Template  Class Template Define Templates: template

4 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

5 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;

6 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!”; } } ;

7

8 STL  Container classes  Sequences  vector, list  Associative Containers  map, set  Container adapters  Stack, queue, priority_queue  String String, rope  bitset  Operation/Utilities  iterator  algorithm http://www.cplusplus.com/

9 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. http://www.cplusplus.com/reference/stl/vector/ 4 5 7 8 9

10 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; } 501 502 503 300 300 200 150 150 100 100 100

11 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; } 1 2 3 4 5 6 Using iterator : 1 2 3 4 5 6

12 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. http://www.cplusplus.com/reference/stl/map/

13 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(…)

14 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); //(12 32 45 71)26 80 53 33 // using function as comp sort (v.begin()+4, v.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp sort (v.begin(), v.end(), myobject); //(80 71 53 45 33 32 26 12) cout << "sort:"; for (it=v.begin(); it!=v.end(); it++) cout << " " << *it; cout << endl; return 0; } sort: 80 71 53 45 33 32 26 12 template void sort ( Iter first, Iter last );

15 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

16 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: http://www.cplusplus.com/

17 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.

18 Reference  http://www.csie.nctu.edu.tw/~jlhuang/course/OOP/notes/chapter10.pdf http://www.csie.nctu.edu.tw/~jlhuang/course/OOP/notes/chapter10.pdf  http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm  http://blog.roodo.com/rocksaying/archives/3641717.html http://blog.roodo.com/rocksaying/archives/3641717.html  http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html  http://www.cplusplus.com/ http://www.cplusplus.com/

19


Download ppt "Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template."

Similar presentations


Ads by Google