Estruturas de dados e algoritmos da STL
Autor(es): Rene Pegoraro, Pedro Henrique Paiola, Wilson M Yonezawa
#include <string>
#include <set>
using namespace std;
int main() {
set <string> nome;
nome.insert("joao");
nome.insert("maria");
nome.insert("jose");
nome.insert("pedro");
nome.insert("maria");
nome.insert("joao");
printf("%d\n", nome.size());
return 0;
}
#include <string>
#include <set>
using namespace std;
struct Par {
int a, b;
Par(int A, int B) : a(A), b(B) {}
bool operator<(const Par &p) const {
return b < p.b;
}
};
struct comp {
bool operator()(const Par &p1,
const Par &p2) {
return p1.a < p2.a;
}
}; // Ordem alternativa indicada pelo “operator()”.
int main() {
set<Par> conj1;
conj1.insert(Par(1, 91));
conj1.insert(Par(2, 52));
conj1.insert(Par(3, 73));
conj1.insert(Par(1, 71));
set<Par, comp> conj2;
conj2.insert(Par(1, 91));
conj2.insert(Par(2, 52));
conj2.insert(Par(3, 73));
conj2.insert(Par(1, 71));
printf("%ld, %ld\n",
conj1.size(), conj2.size());
return 0;
}
#include <cstdio>
#include <map>
#include <string>
using namespace std;
int main () {
map<string,float> not
nota["Pedro"]=10.0;
nota["Antonio"]=5.0;
nota["Maria"]=7.5;
printf("%f\n", nota["Pedro"]);
printf("%f\n", nota["pedro"]);
printf("%f\n", nota["Maria"]);
return 0;
}
#include <vector>
#include <cstdio>
using namespace std;
float vet[] = {7.8, 1.2, 5.6, 9.0, 3.4};
vector<float> vect(&vet[0], &vet[5]);
int main() {
for (vector<float>::iterator it = vect.begin();
it != vect.end(); it++) {
printf("%5.1f", *it);
}
printf("\n");
for (float *it = &vet[0]; it != &vet[5]; it++) {
printf("%5.1f", *it);
}
printf("\n");
for (float f: vect) {
printf("%5.1f", f);
}
printf("\n");
for (float f: vet) {
printf("%5.1f", f);
}
printf("\n");
return 0;
}
float f: vect => range-based for
a partir do cpp11
Obtém-se:
7.8 1.2 5.6 9.0 3.4
7.8 1.2 5.6 9.0 3.4
7.8 1.2 5.6 9.0 3.4
7.8 1.2 5.6 9.0 3.4
#include <string>
#include <set>
using namespace std;
int main() {
set<string> nome;
nome.insert("joao");
nome.insert("maria");
nome.insert("jose");
nome.insert("pedro");
nome.insert("maria");
nome.insert("joao");
set<string>::iterator iter;
for (iter = nome.begin(); iter != nome.end(); iter++) {
printf("%s\n", iter->c_str());
}
return 0;
}
Obtém-se:
joao
jose
maria
pedro
#include <string>
#include <set>
using namespace std;
struct Par {
int a, b;
Par(int A, int B) {
a = A;
b = B;
}
bool operator<(const Par &p)
const {
return a < p.a;
}
};
int main() {
set<Par> conj;
conj.insert(Par(1, 91));
conj.insert(Par(2, 52));
conj.insert(Par(3, 73));
conj.insert(Par(1, 71));
for (set<Par>::iterator it =
conj.begin();
it != conj.end(); it++) {
printf("%d\t%d\n", it->a, it->b);
}
return 0;
}
Obtém-se:
1 91
2 52
3 73
#include <string>
#include <set>
using namespace std;
struct Par {
int a, b;
Par(int A, int B) :
a(A), b(B) {}
bool operator<(const Par &p)
const {
return a < p.a;
}
};
int main() {
multiset<Par> cj;
cj.insert(Par(3, 73));
cj.insert(Par(1, 91));
cj.insert(Par(2, 52));
cj.insert(Par(1, 71));
multiset<Par>::iterator it;
for (it = cj.begin();
it != cj.end(); it++) {
printf("%d\t%d\n", it->a, it->b);
}
return 0;
}
Obtém-se:
1 91
1 71
2 52
3 73
#include <cstdio>
#include <map>
#include <string>
using namespace std;
int main () {
map<string,float> nota;
nota["Pedro"]=10.0;
nota["Antonio"]=5.0;
nota["Maria"]=7.5;
map<string,float>::iterator iter;
for (iter = nota.begin(); iter != nota.end(); iter++) {
printf("%s,\t%f\n", iter->first.c_str(), iter->second);
}
return 0;
}
Obtém-se:
Antonio, 5.000000
Maria, 7.500000
Pedro, 10.000000
#include <string>
#include <set>
using namespace std;
int main() {
set<string> nome;
nome.insert("joao"); nome.insert("maria"); nome.insert("jose");
nome.insert("pedro"); nome.insert("maria"); nome.insert("joao");
for (auto iter = nome.begin(); iter != nome.end(); iter++) {
printf("%s\n", iter->c_str());
}
for (auto iter: nome) { // iter é uma string, não é iterator.
printf("%s\n", iter.c_str());
}
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
int main () {
std::string str ("Test string");
sort(str.begin(), str.end());
for (std::string::iterator it = str.begin(); it != str.end(); ++it)
std::cout << *it;
std::cout << '\n';
return 0;
}
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct Ponto {
float x, y;
Ponto(float X, float Y) :
x(X), y(Y) {};
};
void mostra(vector<Ponto> s) {
vector<Ponto>::iterator it;
printf("-----\n");
for (it = s.begin();
it != s.end(); it++)
printf("%f\t%f\n", it->x, it->y);
}
bool operator<(const Ponto &p1,
const Ponto &p2) {
return p1.x < p2.x;
}
int main() {
vector<Ponto> lPt;
lPt.push_back(Ponto(1.2, 2.3));
lPt.push_back(Ponto(4.5, 5.6));
lPt.push_back(Ponto(1.2, 1.2));
lPt.push_back(Ponto(6.7, 7.8));
mostra(lPt);
stable_sort(lPt.begin(), lPt.end());
mostra(lPt);
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
bool ehMaior(int i) { return i > 10; }
int main() {
int vet[] = { 10, 90, 4, -10, 25, 93, 10, 7, 0, 33 };
for (int i=0; i<10; i++)
printf("%3d ", vet[i]);
printf("\n");
replace_if(vet, vet+10, ehMaior, 10);
for (int i=0; i<10; i++)
printf("%3d ", vet[i]);
return 0;
}
Obtém-se:
10 90 4 -10 25 93 10 7 0 33
10 10 4 -10 10 10 10 7 0 10
#include <deque>
#include <algorithm>
#include <string>
using namespace std;
bool iniciaComJ(string s) {
return s[0] == 'J';
}
int main() {
deque<string> vet;
vet.push_back("Joao");
vet.push_back("Maria");
vet.push_back("Pedro");
vet.push_back("Jose");
vet.push_back("Antonio");
for (auto i = vet.begin();
i!=vet.end(); i++)
printf("%s ", i->c_str());
printf("\n");
replace_if(vet.begin(), vet.end(),
iniciaComJ, "XXXX");
for (auto i = vet.begin();
i!=vet.end(); i++)
printf("%s ", i->c_str());
return 0;
}
Obtém-se:
Joao Maria Pedro Jose Antonio
XXXX Maria Pedro XXXX Antonio
#include <algorithm>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> vet1;
vet1.push_back("banana");
vet1.push_back("pera");
vet1.push_back("maça");
string vet2[] = {"abacaxi", "laranja", "pera"};
list<string> l;
set_difference(vet1.begin(), vet1.end(),
vet2, vet2 + 3,
front_inserter(l));
. . .
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main() {
vector<string> vet;
vet.push_back("Joao");
vet.push_back("Maria");
vet.push_back("Pedro");
vet.push_back("Jose");
vet.push_back("Antonio");
sort(vet.begin(), vet.end(),
greater<string>());
for (auto i = vet.begin();
i!=vet.end(); i++)
printf("%s ", i->c_str());
printf("\n");
return 0;
}
No vídeo, obtém-se:
Joao Maria Pedro Jose Antonio
XXXX Maria Pedro XXXX Antonio