1 / 19

OO 演習課 20 10/06/10

OO 演習課 20 10/06/10. 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.

Download Presentation

OO 演習課 20 10/06/10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OO演習課2010/06/10 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. temp_class<int> a; temp_class<char> b; … …

  3. Templates Two types of templates: • Function Template • Class Template Define Templates: template<class T> template<class T=float>

  4. Template’s Example void main() { X<int, float> x1; //T1=int, T2=float X<char, double*> x2; //T1=char, T2= double* int i=3; fun( i); //T3=int } template<class T1, class T2> class X ; //Class Template template<typename T3> void fun(T3 data) {} //Function Template //class and typename are the same

  5. Partial Specialization template<class T1, class T2> class X { public: X() { cout << “Construct”; } void display(T x) { cout << “Origin!”; } } ; template<class T1> class X <T, int> { public: X() { cout << “Construct”; } void display(int x) { cout << “Specialize!!”; } } ; X<float,float> t1; X<float,int> t2;

  6. Function Specialization void X<int>::display(int x) { cout << “Integer!!”; } void X<char>::display(int x) { cout << “Character!!”; } template<class T> class X { public: X() { cout << “Construct”; } void display(T x) { cout << “Origin!”; } } ;

  7. C++ STL (Standard Template Library)

  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 <iostream> #include <vector> using namespace std; int main () { vector<int> 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. 4 5 7 8 9 http://www.cplusplus.com/reference/stl/vector/

  10. Using iterator #include <iostream> #include <vector> using namespace std; int main () { vector<int> v1 (3,100); vector<int>::iterator it; it = v1.begin(); it = v1.insert (it,200); v1.insert (it,2,300); // "it" no longer valid it = v1.begin(); vector<int> 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 <iostream> #include <vector> using namespace std; int main () { vector< vector<int> > d2( 2,vector<int>(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< vector< vector<int> > > d3; d3.push_back(d2); cout << "Using iterator :" << endl; vector<vector< vector<int> > >::iterator it1; vector<vector<int> >::iterator it2; vector<int>::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 #include <iostream> #include <map> #include <string> using namespace std; int main () { map<char,string> 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; } Maps are a kind of associative containers that stores elements formed by the combination of a key valueand a mapped value. '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 <algorithm> 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 template <class Iter> void sort ( Iter first, Iter last ); #include <iostream> #include <algorithm> #include <vector> 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<int> v(myints, myints+8); vector<int>::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

  15. Search example #include <iostream> #include <algorithm> #include <vector> using namespace std; bool myfunction (int i,int j) { return (i==j); } int main () { vector<int> v; vector<int>::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<string>("he", "llo"); string s2= plus<string>( s1 , " again"); string s3= plus<string>( "b", "ye!"); vector<string> 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.iis.sinica.edu.tw/~kathy/vcstl/templates.htm http://blog.roodo.com/rocksaying/archives/3641717.html http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html http://www.cplusplus.com/

  19. Question?

More Related