ikp213-06-template-c++

843 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

ikp213-06-template-c++

  1. 1. BAHASA PEMROGRAMAN(IKP213)Pertemuan 6: Template C++, 6 Desember 2011
  2. 2. Function Overload2 void printData(int value) { cout<<"The value is "<<value<<endl; }  Bagaimana untuk mencetak tipe string? int?  Overload: Buat beberapa fungsi dengan signature berbeda void printData(string value) { cout<<"The value is "<<value<<endl; } STL – IKP213
  3. 3. Template3 template<typename T> void printData(T value) { cout<<"The value is "<<value<<endl; }  Mendefinisikan T sebagai sebuah tipe template  T akan diinstantiasi sesuai kebutuhan double d = 10.01; string s = "Template"; printData(d); printData(s); STL – IKP213
  4. 4. Template Parameter4  Sintaks penulisan template<typename name> template<class name>  Pemanggilan fungsi / class akan diinstantiasi sesuai tipe yang digunakan template<typename T> void func(T value) { const T& ref=value; T* p=new T; T temp(23); } STL – IKP213
  5. 5. Template Parameter5  Jika ada definisi func<int>, akan digenerate sebuah instantiasi fungsi void func(int value) { const int& ref=value; int* p=new int; int temp(23); }  Semua kemunculan T digantikan int STL – IKP213
  6. 6. Template Parameter6  Jika ada definisi func<string>, akan muncul kesalahan kompilasi void func(string value) { const string& ref=value; string* p=new string; string temp(23); }  Karena string temp(23) tidak valid STL – IKP213
  7. 7. Standard Template Library (STL)7  Koleksi containers dan algorithms  Containers  Class untuk menampung sekumpulan objek  vector, list, queue, deque, priority_queue, stack, map, multimap, set, multiset STL – IKP213
  8. 8. STL8  Deklarasi vector<int> dataInt(5); vector<string> dataStr(5);  Assignment dataInt[0] = 10; dataStr[3] = "Budi";  Menambahkan data vector<string> vectS; vectS.push_back("Kata"); vectS.push_back("siapa"); vectS.push_back("STL susah"); STL – IKP213
  9. 9. Loop by Index9 int ii; for (ii=0; ii < SS.size(); ii++) { cout << SS[ii] << endl; } STL – IKP213
  10. 10. Loop by Iterator10 vector<string>::const_iterator cii; for (cii=SS.begin(); cii!=SS.end(); cii++) { cout << *cii << endl; } STL – IKP213
  11. 11. Loop by Reverse Iterator11 vector<string>::reverse_iterator rii; for (rii=SS.rbegin(); rii!=SS.rend(); ++rii) { cout << *rii << endl; } STL – IKP213
  12. 12. Matriks12 // Vector length of 3 initialized to 0 vector<int> vI1Matrix(3,0); // Vector length of 4 initialized to hold another // vector vI1Matrix which has been initialized // to 0 vector< vector<int> > vI2Matrix(4, vI1Matrix); // Vector of length 5 containing two dimensional // vectors vector< vector< vector<int> > > vI3Matrix(5, vI2Matrix); STL – IKP213
  13. 13. Dalam satu baris13 vector< vector< vector<int> > > vI3Matrix(2, vector< vector<int> > (3, vector<int>(4,0)) ); STL – IKP213
  14. 14. Iterator Matriks14 // Declare two dimensional array vector< vector<int> > vI2Matrix; vector<int> A, B; vector< vector<int> >::iterator iter_ii; vector<int>::iterator iter_jj; A.push_back(10); A.push_back(20); A.push_back(30); B.push_back(100); B.push_back(200); B.push_back(300); vI2Matrix.push_back(A); vI2Matrix.push_back(B); STL – IKP213
  15. 15. Iterator Matriks15 cout << "Using Iterator:" << endl; for(iter_ii=vI2Matrix.begin(); iter_ii!=vI2Matrix.end(); iter_ii++) { for(iter_jj=(*iter_ii).begin(); iter_jj!=(*iter_ii).end(); iter_jj++) { cout << *iter_jj << endl; } } STL – IKP213
  16. 16. Operator-operator Lain16  empty()  size()  resize(n)  front(), v[0]  back()  at(index), v[index]  erase(), clear() STL – IKP213
  17. 17. Pustaka17  STL Tutorial, http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html  STL Tutorial, http://www.mochima.com/tutorials/STL.html  STL Reference, http://www.cplusplus.com/reference/stl/  http://tjerdastangkas.blogspot.com/search/label/ikp213 STL – IKP213
  18. 18. AKHIR PERTEMUAN 6Selasa, 6 Desember 2011

×