Your SlideShare is downloading. ×
  • Like
ikp213-06-template-c++
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

ikp213-06-template-c++

  • 423 views
Published

 

Published in Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
423
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
19
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. BAHASA PEMROGRAMAN(IKP213)Pertemuan 6: Template C++, 6 Desember 2011
  • 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. 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. 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. 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. 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. 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. 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. Loop by Index9 int ii; for (ii=0; ii < SS.size(); ii++) { cout << SS[ii] << endl; } STL – IKP213
  • 10. Loop by Iterator10 vector<string>::const_iterator cii; for (cii=SS.begin(); cii!=SS.end(); cii++) { cout << *cii << endl; } STL – IKP213
  • 11. Loop by Reverse Iterator11 vector<string>::reverse_iterator rii; for (rii=SS.rbegin(); rii!=SS.rend(); ++rii) { cout << *rii << endl; } STL – IKP213
  • 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. Dalam satu baris13 vector< vector< vector<int> > > vI3Matrix(2, vector< vector<int> > (3, vector<int>(4,0)) ); STL – IKP213
  • 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. 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. Operator-operator Lain16  empty()  size()  resize(n)  front(), v[0]  back()  at(index), v[index]  erase(), clear() STL – IKP213
  • 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. AKHIR PERTEMUAN 6Selasa, 6 Desember 2011