Documente Academic
Documente Profesional
Documente Cultură
Library - STL
Ce oferă bibliotecile standard C++?
header
#include <string>
spaţiul de nume folosit
using namespace std;
declaraţii şiruri:
string s("Un text simplu.");
string tag("$tag$");
Un text simplu.
s 15
$tag$
tag 5
#include <iostream>
#include <string>
int main() {
string quote1("There are two sides to every issue: one side is right\n");
string quote2("and the other is wrong, but the middle is always evil.");
quote1.append(quote2);
cout << quote1 << endl;
quote1.erase(34, 42);
cout << quote1 << endl;
return 0;
}
int main() {
string quote1("A little learning is a dangerous thing.");
cout << (unsigned int)quote1.find('i') << endl;
return 0;
}
Queue
Stack Adaptori de containere
Priority queue
Set
Map
Multiset Containere asociative (cheie-valoare)
Multimap
Bitset
Standard Template Library 12
STL - Vector
Elimină problema realocării dinamice a spaţiului de memorie.
Se redimensionează în partea finală astfel încât să poată fi realizată
adăugarea de noi elemente.
Compus dintr-un bloc de memorie alocată secvenţial.
Timp constant pentru inserţie şi eliminarea componentelor de la sfârşit.
Timp liniar pentru inserarea şi eliminarea elementelor de la început şi interior.
Devine ineficient când depăşirile de memorie alocată pot determina copierea
întregului vector.
#include <vector>
std::vector<int> tab; Declaraţie vector de numere întregi
#include <deque>
std::deque<int> deque; Declaratie coadă de numere întregi
erase 2 forme – şterge elementul indicat de iterator sau O(1) sau O(n)
secvenţa dintre 2 iteratori
sort sortează elementele cozii în ordine ascendentă. O(nlog2n)
Operatorul ‘<‘ trebuie să fie supraîncărcat
reverse inversează ordinea elementelor unei cozi O(n)
merge interclasează elementele a două cozi. În urma O(m+n)
operaţiei, elementele din coada argument sunt m,n lungimile
inserate în coada apelantă. Ambele cozi trebuie să cozilor
fie ordonate.
#include <stack>
#include <list>
std::stack<int, std::list<int>> stiva;
Implicit deque (lista consumă mai multă memorie, vectorii se expandează mai greu)
std::stack<int> stiva;
Operaţie Descriere
empty Returnează true dacă stiva este vidă
int main(){
set<int> intSet;
for(int i = 0; i < 25; i++)
for(int j = 0; j < 10; j++)
intSet.insert(j); 0123456789
set<int>::iterator it = intSet.begin();
while(it != intSet.end()){
cout << *it << " ";
it++;
}
return 0;
}
Standard Template Library 30
STL – Multiset
Permite apariţia unui element de mai multe ori.
multiset<int>::iterator it = intSet.begin();
while(it != intSet.end()){
cout << *it << " ";
it++;
}
return 0;
}
pentru ieşire.
ifstream fin("date.in");
istream_iterator<int> fiit(fin); //file iterator
ostream_iterator while (fiit != eos) {
a.push_back(*fiit);
istream_iterator fiit++;
}
ofstream fout("date.out");
ostream_iterator<int> fout_it(fout,"|");
copy(a.begin(), a.end(), fout_it);
return 0;
}
Standard Template Library 44
STL – Iteratori de inserare
x iterator
*x = 3 Suprascrie valoarea existentă
int main() {
vector<int> v1, v2; 12345 v1
for (int i = 1; i <= 5; i++) {
v1.push_back(i);
10 20 30 40 50 v2
v2.push_back(i*10);
}
int main() {
deque<int> deque1, deque2; 12345 deque1
for (int i = 1; i <= 5; i++) {
deque1.push_back(i);
10 20 30 40 50 deque2
deque2.push_back(i*10);
}
return 0;
}
Standard Template Library 47
STL – insert iterator
#include <iostream>
#include <iterator>
#include <list>
using namespace std;
it
int main() {
list<int> list1, list2;
for (int i = 1; i <= 5; i++) { 12345 list1
list1.push_back(i);
list2.push_back(i*10);
} 10 20 30 40 50 list2
list<int>::iterator it;
it = list1.begin();
advance(it, 3);
class PersonSortCriterion {
public:
bool operator() (const Person& p1, const Person& p2) const {
return p1.lastname() < p2.1astname() ||
((p2.1astname() == p1.lastname()) &&
p1.firstname() < p2.firstname());
}
};
int main() {
//se declara tipul set avand un o functie speciala de comparare
typedef set<Person,PersonSortCriterion> PersonSet;
//se creaza o colectie de acest tip
PersonSet coll;
...
//se prelucreaza elementele colectiei
PersonSet::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
...
}
...
}
Standard Template Library 52
STL – Obiecte de tip funcţie
Exemplu: un obiect de tip funcţie folosit pentru a genera o secvenţă
de numere întregi.
class IntSequence {
private:
int value;
public:
IntSequence (int initialValue) : value(initialValue) { }
//operatorul supraincarcat
int operator() () {
return value++;
}
};
return 0;
}
return 0;
}
int main () {
template <class InputIterator, class T> vector<int> v;
typename v.push_back(-10);
iterator_traits<InputIterator>::differenc v.push_back(-95);
e_type count ( ForwardIterator first, v.push_back(40);
ForwardIterator last, const T& value ); v.push_back(-55); -10 -95 40 -55
int main() {
vector<int> v;
for (int i = 1; i < 6; i++)
v.push_back(i*10); Comparaţie implicită (==)
int a[] = {10, 20, 80, 320, 1024};
pair<vector<int>::iterator, int*> mypair;
mypair = mismatch(v.begin(), v.end(), a);
cout << "First mismatching elements: " << *mypair.first;
cout << " and " << *mypair.second << endl;;
mypair.first++; mypair.second++;
mypair = mismatch(mypair.first, v.end(), mypair.second, pred);
cout << "Second mismatching elements: " << *mypair.first;
cout << " and " << *mypair.second << endl;
Comparaţie cu predicat
return 0; First mismatching elements: 30 and 80
} Second mismatching elements:
Standard Template 40 and 320
Library 63
STL – search
Caută prima apariţie a unei bool pred(int i, int j) { return (i==j); }
secvenţe în altă secvenţă şi
returnează un iterator pe primul int main() {
element al secvenţei (comparaţia vector<int> v;
10 20 30 40 50 60 70 80 90
se face folosind ‘==‘ sau un vector<int>::iterator it;
predicat). for (int i=1; i<10; i++) v.push_back(i*10);
return 0;
}
vector<int>::iterator it; 10 99 30 30 99 10 10 99
for (it = v.begin(); it != v.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
fill_n(v2.begin()+3, 3, 33); 20 20 20 33 33 33 10 10
generate(v.begin(), v.end(),RandomNumber);
Completează elementele for (it = v.begin(); it != v.end(); ++it)
dintre first şi last cu
cout << " " << *it;
valorile generate de funcţia cout << endl;
gen.
int a[9];
generate_n(a, 9, UniqueNumber);
for (int i = 0; i < 9; ++i)
cout << " " << a[i];
return 0;
}
Standard Template Library 73
STL – remove
template < class ForwardIterator, #include <iostream>
class T > ForwardIterator remove #include <algorithm>
( ForwardIterator first, using namespace std;
ForwardIterator last, const T&
value ); int main() {
int a[] = {10,20,30,40,50,20,70};
Elimină value din intervalul 10 20 30 40 50 20 70
[first,last). int* pbegin = a;
int* pend = a + sizeof(a)/sizeof(int);
Returnează un iterator spre pend = remove(pbegin, pend, 20);
noul sfârşit al secvenţei.
cout << “Intervalul contine: ";
for (int* p = pbegin; p != pend; ++p)
cout << " " << *p;
cout << endl; 10 30 40 50 70
return 0;
}
return 0;
}
return 0;
}
return 0;
}
Standard Template Library 80
STL – Sortări
sort
stable_sort
partial_sort
partial_sort_copy
n_th_element
partial_sort(v.begin(), v.begin()+5,
Sortează elementele din v.end(), comp);
domeniul [first,last) 123459876
astfel încât elementele din for (it = v.begin(); it != v.end(); ++it)
[first, middle) conţine cout << " " << *it;
elementele sortate crescător, cout << endl;
iar restul nu sunt ordonate.
return 0;
}
Standard Template Library 83
STL – nth element
template <class bool comp(int i, int j) { return (i<j); }
RandomAccessIterator> void
nth_element ( RandomAccessIterator int main() {
first, RandomAccessIterator nth,
RandomAccessIterator last ); vector<int> v;
vector<int>::iterator it;
template <class
RandomAccessIterator, class for (int i = 1; i < 10; i++)
Compare> void nth_element ( v.push_back(i);
RandomAccessIterator first,
RandomAccessIterator nth, //random_shuffle(v.begin(), v.end());
RandomAccessIterator last, Compare
comp) nth_element(v.begin(), v.begin()+5,
v.end());
Rearanjează elementele dintre
first şi last astel încât nth nth_element(v.begin(), v.begin()+5,
v.end(), comp);
va reprezenta elementul de pe
poziţia n într-o ordonare
for (it = v.begin(); it != v.end();++it)
crescătoare a elementelor,
cout << " " << *it;
fără a garanta că elementele
cout << endl;
anterioare sau posterioare ar fi
ordonate.
return 0;
}
Standard Template Library 84
STL – Căutare binară
lower_bound
upper_bound
equal_range
binary_search
return 0;
} Template Library
Standard 87
STL – algoritmi de interclasare
merge
inplace_merge
includes
set_union
set_intersection
set_difference
set_symmetric_difference
push_heap
pop_heap
make_heap
sort_heap
make_heap(v.begin(), v.end());
cout << "initial max heap : " << v.front() << endl; 30 20 15 10 5
pop_heap(v.begin(),v.end());
v.pop_back();
cout << "max heap after pop : " << v.front() << endl;
20 15 10 5 30 20 15 10 5
v.push_back(99);
push_heap(v.begin(),v.end()); 20
cout << "max heap after push: " << v.front() << endl;
99 20 15 10 5
sort_heap(v.begin(),v.end());
return 0;
}
Standard Template Library 93
STL – algoritmi de minim / maxim
min
max
min_element
max_element
lexicographical_compare
next_permutation
prev_permutation