Biblioteca padrão do C++ que inclui diferentes estruturas de dados e algoritmos
Autor(es): Rene Pegoraro, Pedro Henrique Paiola, Wilson M Yonezawa
C = amortized constant time
http://www.tantalon.com/pete/cppopt/appendix.htm
#include <cstdio>
#include <vector>
using namespace std;
int main() {
vector<float> vf;
vf.push_back(1.23);
vf.push_back(9.87);
vf.push_back(6.45);
printf("%f, %f, %f, %f, %d\n",
vf[0], vf[1], vf[2], vf[10], vf.size());
printf("%f\n", vf.back());
vf.pop_back();
printf("%f, %f, %f, %d\n", vf[0], vf[1], vf[2], vf.size());
return 0;
}
Obtém-se:
1.230000, 9.870000, 6.450000, 0.000000 (lixo), 3
6.450000
1.230000, 9.870000, 6.450000 (lixo), 2
#include <cstdio>
#include <list>
using namespace std;
struct Ponto {
float x, y;
Ponto(float X, float Y) {
x = X;
y = Y;
}
};
// Obs: o operador [ ] não pode ser usado em list
int main() {
list<Ponto> lPt;
Ponto p(1.2, 2.3);
lPt.push_back(p);
lPt.push_front(Ponto(4.5, 5.6));
lPt.push_back(Ponto(6.7, 7.8));
int n = lPt.size();
for (int i = 0; i < n; i++) {
printf("(%f, %f)\n", lPt.front().x, lPt.front().y);
lPt.pop_front();
}
return 0;
}
Obtém-se:
(4.500000, 5.600000)
(1.200000, 2.300000)
(6.700000, 7.800000)
#include <cstdio>
#include <list>
using namespace std;
struct Ponto {
float x, y;
Ponto(float X, float Y) {
x = X;
y = Y;
}
};
bool ordem(Ponto &p1, Ponto &p2) {
return p1.x < p2.x;
}
int main() {
list<Ponto> lPt;
lPt.push_back(Ponto(1.2, 2.3));
lPt.push_front(Ponto(4.5, 5.6));
lPt.push_back(Ponto(6.7, 7.8));
lPt.sort(ordem);
int n = lPt.size();
for (int i = 0; i < n; i++) {
printf("(%f, %f)\n", lPt.front().x, lPt.front().y);
lPt.pop_front();
}
return 0;
}
Obtém-se:
(1.200000, 2.300000)
(4.500000, 5.600000)
(6.700000, 7.800000)
#include <cstdio>
#include <list>
using namespace std;
struct Ponto {
float x, y;
Ponto(float X, float Y) {
x = X;
y = Y;
}
bool operator<(Ponto &p) {
return x < p.x;
}
};
// O “operator<” determina a ordem natural dos Ponto’s
int main() {
list<Ponto> lPt;
lPt.push_back(Ponto(1.2, 2.3));
lPt.push_front(Ponto(4.5, 5.6));
lPt.push_back(Ponto(6.7, 7.8));
lPt.sort();
int n = lPt.size();
for (int i = 0; i < n; i++) {
printf("(%f, %f)\n", lPt.front().x,
lPt.front().y);
lPt.pop_front();
}
return 0;
}
Obtém-se:
(1.200000, 2.300000)
(4.500000, 5.600000)
(6.700000, 7.800000)
list<string>
para definir a lista.splice
Move elementos de uma lista para outraremove
Remove elementos com valor específicoremove_if
Remove elementos com condiçãomerge
Junta listas ordenadassort
Ordena elementos no containerreverse
Inverte a ordem dos elementos[]
empty
testa se o container está vaziosize
retorna o tamanhotop
faz acesso ao próximo elementopush
adiciona um elemento ao topopop
remove o elemento do topoempty
Testa se o container está vaziosize
Retorna o tamanhofront
Faz acesso ao primeiro elementoback
Faz acesso ao último elementopush
Insere um elemento no final da fila#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
struct Pessoa {
char nome[30];
float salario;
Pessoa(char nome[],
float sal) {
strcpy(this->nome, nome);
salario = sal;
}
};
int main () {
queue<Pessoa> fila;
fila.push(Pessoa("Rene", 10.00));
fila.push(Pessoa("Maria", 80.00));
fila.push(Pessoa("Jose", 50.00));
while (!fila.empty()) {
printf("%s ", fila.front().nome);
fila.pop();
}
return 0;
}
int main () {
queue<Pessoa> fila;
fila.push(Pessoa("Rene", 10.00));
fila.push(Pessoa("Maria", 80.00));
fila.push(Pessoa("Jose", 50.00));
while (!fila.empty()) {
printf("%s ", fila.front().nome);
fila.pop();
}
return 0;
}
#include <cstdio>
#include <queue>
#include <string>
using namespace std;
struct Pessoa {
string nome; float sal;
Pessoa(string nome, float sal) {
this->nome = nome;
this->sal = sal;
}
bool operator<(Pessoa &p) {
return sal < p.sal;
}
};
int main () {
priority_queue<Pessoa> fila;
fila.push(Pessoa("Pedro", 10.00));
fila.push(Pessoa("Maria", 80.00));
fila.push(Pessoa("Jose", 50.00));
while (!fila.empty()) {
printf("%s ", fila.top().nome.c_str());
fila.pop();
}
return 0;
}
Obtém-se:
Maria Jose Pedro
priority_queue<string>
para definir a lista.