STL : Standard Template Library

download STL : Standard Template Library

If you can't read please download the document

description

STL : Standard Template Library

Transcript of STL : Standard Template Library

  • 1. STL : Standard Template Library Mario Ynocente Castro UNI-FIIS

2. Referencia

    • http://www.sgi.com/tech/stl/
    • http://www.cppreference.com/wiki/stl/start
    • http://www.cplusplus.com/reference/stl/

3. Ventajas

    • Estandarizada
    • Eficiente
    • Pequea, fcil de aprender
    • Flexible
    • Cdigo abierto

4. Notas Los #include no llevan .h #include // new include method #include // vector container #include // STL algorithms using namespace std; // assume std:: 5. Contenedores

    • Contienen elementos
    • Proveen iteradores que apuntan a sus elementos
    • Proveen un conjunto mnimo de operaciones mnimas para manipular sus elementos

6. Vector vector v(10); for(int i = 0; i < 10; i++) {v[i] = (i+1)*(i+1); } int numero_elementos = v.size(); bool esta_vacio = (v.size()==0); // Evite usar esto bool esta_vacio = ! v.empty(); 7. Vector vector v; // ...vector v2(v);vector v3(v.begin(), v.end()); //v3 igual a v2 int data[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };vector primes(data, data+(sizeof(data) / sizeof(data[0]))); vector v;// ...vector v2(v.begin(), v.begin() + (v.size()/2)); 8. Vector vector v; // ... vector v2(v.rbegin()+(v.size()/2), v.rend()); vector v; // ...// Traverse all container, from begin() to end() vector::iterator it; // declaracin de un iterador for(it= v.begin(); it != v.end(); it++) {*it++; // Increment the value iterator is pointing to} 9. Vector vector v(20); for(int i = 0; i < v.size(); i++) { v[i] = i+1; } v.resize(25); for(int i = 20; i < 25; i++) { v[i] = i*2; } 10. Vector vector< vector > Matrix; // >> int N, M; // ...vector< vector > Matrix(N, vector(M, -1)); vector v1;// ...vector v2 = v1; vector v3(v1); 11. Bsqueda en Vector vector v;for(int i = 1; i < 100; i++) {v.push_back(i*i);}Vector :: iterator it= find(v.begin(), v.end(), 49); if(it != v.end()) {//...} 12. Insercin en Vector vector v; // ...v.insert(1, 42); // Insert value 42 after the first vector v;vector v2;// ... v.insert(1,v2.begin(),v2.end()); 13. Eliminacin en Vector erase(iterator);erase(iterador inicial, iterador final); 14. String string s = "hello";strings1 = s.substr(0, 3), // "hel"s2 = s.substr(1, 3), // "ell"s3 = s.substr(0, s.length()-1), "hell"s4 = s.substr(1); // "ello" 15. Pair template struct pair { T1 first; T2 second; }; pair > P; string s = P.first; // extraer string int x = P.second.first; // extraer primer entero int y = P.second.second; // extract segundo entero 16. Set

      • Agregar elementos
      • No permite duplicados
      • Remover elementos
      • Nmero de elementos distintos
      • Verificar si un elemento est en el Set

17. Set set s;for(int i = 1; i tmp) { v.push_back(tmp);}} 26. String Streams string f(const vector& v) {ostringstream os;for(vector::iterator it=v.begin();it!=v.end();it++){ os