STL
STL. lekt.V.Giedrimas. Planas. Šablonai STL biblioteka. Templates. Templates allow functions and classes to be parameterized so that the type of data being operated upon (or stored) is received via a parameter .
STL
E N D
Presentation Transcript
STL lekt.V.Giedrimas
Planas • Šablonai • STL biblioteka
Templates Templates allow functions and classes to be parameterizedso that the type of data being operated upon (or stored) is received via a parameter. Templates provide a means to reuse code — one template definition can be used to create multiple instances of a function (or class), each operating on (storing) a different type of data. The template mechanism is important and powerful. It is used throughout the Standard Template Library (STL) to achieve genericity.
int temp = x;x = y;y = temp void Swap(int & first, int & second){int temp = first; first = second; second = temp; } Sveikų skaičių sukeitimas
Realių skaičių sukeitimas void Swap(double & first, double & second){double temp = first; first = second; second = temp; }
Eilučių sukeitimas void Swap(string& first, string & second){string temp = first; first = second; second = temp; }
Šablonas template <typename DataType > void Swap(DataType& first, DataType & second){DataType temp = first; first = second; second = temp; }
Šablono naudojimas int i1, i2 double d1, d2; string s1, s2; Time t1, t2; ... Swap<int>(i1, i2); Swap<double>(d1, d2); Swap <string>(s1, s2); ...
STL (Standard Template Library) • Standartinių šablonų biblioteka, kurioje yra tokios šablonų grupės: • Duomenų tipai (Container Classes) • Nuoseklūs konteineriai: vektoriai, sąrašai ir t.t. (sequential containers) • Asociatyvūs konteineriai: aibės ir t.t. (associative containers) • Iteratoriai (Iterators) • Algoritmai (Algorithms)
Nuoseklių konteinerių klasės • Skirtos elementams saugoti išlaikant jų eiliškumą: galima vienareikšmiškai pasakyti kuris elementas po kurio seka. • Yra 3 klasės:
Asociatyvių konteineriųklasės • Elementai neturi griežtai nustatyto eiliškumo • Elementai gali būti randami ne pagal jų vietą, bet ir pagal vardą
Iteratoriai • Iteratorius – rodyklės tipo kintamasis rodantis į kurią nors kitos duomenų struktūros (pvz.: sąrašo) elementą • Siekiant pereiti prie sekančio elemento pakanka iteratoriaus reiškę padidinti (operatoriumi ++) arba sumažinti (operatoriumi--) vienetu • Siekiant perskaityti elementą, į kurį rodo iteratoius reikia naudoti žvaigdutę (*) prieš jo vardą (taip kaip ir dirbant su rodyklėmis)
Iteratoriai • Iteratorių rūšys;
Algoritmai • STL leidžia pasinaudoti paprasčausiais, tačiau dažnai naudojamais algoritmais
Pavyzdys suvectorklase #include <iostream> #include <vector> // pasakoma, kad bus naudojami vektoriai using namespace std; int main() { vector<int> vect; // Aprašomas vektorius, kurio elementai int tipo vector<int>::iterator iter for (int x = 0; x < 10; x++) vect.push_back(x*x); //Ekrane spausdinamas vektoriaus turinys iter= vect.begin(); while (iter != vect.end()) { cout << *iter << " "; iter ++; } }
Pavyzdys sulistklase #include <iostream> #include <list> using namespace std; int main() { list<int> myList; list<int>::iterator iter; // Įvedami duomenys for (int x = 0; x < 100; x += 10) myList.push_back(x); //Ekrane spausdinamas sąrašo turinys for (iter = myList.begin(); iter != myList.end(); iter++) cout << *iter << " "; cout << endl; // Elementai išrikiuojami priešingai myList.reverse(); //Vėl ekrane spausdinamas sąrašo turinys for (iter = myList.begin(); iter != myList.end(); iter++) cout << *iter << " "; cout << endl; }
Pavyzdys supaieškos algoritmu #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int x; vector<int> vect; for (x = 0; x < 10; x++) vect.push_back(x); cout << “Vektoriuje yra " << vect.size() << “elementų:\n”; for (x = 0; x < vect.size(); x++) cout << vect[x] << " "; cout << endl; // tęsinys kitoje skaidrėje
Pavyzdys supaieškos algoritmu (tęsinys) // Atsitiktinai sumaišo elementus random_shuffle(vect.begin(), vect.end()); // Parodo ekrane cout << "The elements have been shuffled:\n"; for (x = 0; x < vect.size(); x++) cout << vect[x] << " "; cout << endl; // Rikiuoja elementus. sort(vect.begin(), vect.end()); // Vėl parodo ekrane for (x = 0; x < vect.size(); x++) cout << vect[x] << " "; cout << endl; // Ieško elementų if (binary_search(vect.begin(), vect.end(), 7)) cout << “Skaičiaus 7 vektoriuje nėra.\n"; else cout << " Skaičius 7 vektoriuje yra!\n"; return 0; }
Literatūra • Ли М., Массер Д., Плаугер П., Степанов А.STL - стандартная библиотека шаблонов С++"BHV-Санкт-Петербург" · 2004 • http://books.dore.ru/bs/f6sid271.html • http://cwx.prenhall.com/bookbind/pubbooks/ford2/
STL lekt.V.Giedrimas