Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
STL
STL Containers
Sequence Container
Stores data by position in linear order: First element, second element , etc:
Associate Container
Stores elements by key, such as name, social security number or part number Access an element by its key which may bear no relationship to the location of the element in the container
Adapter Container
Contains another container as its underlying storage structure
STL Containers
Vector Stack Queue Set, multiset Map, multimap
Vector Container
Generalized array that stores a collection of elements of the same data type Vector similar to an array
Vectors allow access to its elements by using an index in the range from 0 to n-1 where n is the size of the vector
Vector vs array
Vector has operations that allow the collection to grow and contract dynamically at the rear of the sequence
Vector
Example: Library:
#include <vector>
Declaration:
vector<int>scores(100);//100 integer scores
Vector
#include <cstdio> #include <vector> using namespace std; int main() { vector<int> v(5, 5); printf("v[2] = %d\n", v[2]); for (int i = 0; i < 5; i++) { v[i] = i; } printf("v[2] = %d\n", v[2]); v.push_back(5); printf("v[5] = %d\n", v[5]); return 0; }
// 5
// 2
// 5
Vector
//Iterator vector<int>::iterator it; //Sort sort(v.rbegin(), v.rend()); for (it = v.begin(); it != v.end(); it++) printf("%d ", *it);
Stack
A data structures which only allow insertion (push) and deletion (pop) from the top only. Stack
allows access at only one end of the sequence (top) Adds objects to container by pushing the object onto the stack Removes objects from container by popping the stack LIFO ordering (last end, first out)
Stack
Important stack operations: 1. Push (C++ STL: push())
Adds new item at the top of the stack.
Example
#include <stdio.h> #include <stack> using namespace std; void main() { stack<int> s; s.push(3); s.push(1); s.push(2); s.push(7); s.push(6); s.push(5); s.push(4); while (!s.empty()) { printf("%d ",s.top()); s.pop(); } printf("\n"); return 0; }
Ejercicio
Programa que realice n sumas de dos nmeros tan grandes que no quepan en ningn tipo de dato, utilice pilas.
Entrada: Salida:
2 1234 23 2345 34
1257 2379
Queue
A data structures which only allow insertion from the back (rear), and only allow deletion from the head (front). This behavior is called First In First Out (FIFO), similar to normal queue in the real world. Queue
Allows access only at the front and rear of the sequence Items enter at the rear and exit from the front FIFO ordering (first-in first-out ) push(add object to a queue) pop (remove object from queue)
#include <stdio.h> #include <queue> using namespace std; void main() { queue<int> q; q.push(3); q.push(1); q.push(2); q.push(7); q.push(6); q.push(5); q.push(4); while (!q.empty()) { printf("%d ",q.front()); q.pop(); } printf("\n"); return 0; }
A simple calculator
Infix to posfix
Evaluating postfix
Tarea
1100 - Transform the Expression