Programming and Data Structures Programming and Data Structures
Lecture 11 Lecture 11
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Standard Template Library (STL) Standard Template Library (STL)
●
input/output streams: iostream, fstream, sstream, iomanip
●
string management: string
●
standard exceptions: stdexcept
●
containers:
●
sequence containers: vector, deque, list
●
container adapters: stack, queue, priority_queue
●
associative containers: set, map
●
iterators: iterator
●
advanced numerics: complex, numeric, limit
●
generalized algorithms: algorithm, functional
vector vector
●
#include <vector>
●
sequence container – dynamic array using contiguous storage
●
fast access by indexing operator []
●
fast addition/removal at the end of the sequence: push_back, pop_back
●
slow insertion/removal at positions other than the end: insert, erase (memory reallocation)
vector<int> v;
v.push_back(1);
v.push_back(3);
cout << v.size(); //2 cout << v[1]; //3 v[0] = 5;
cout << v[0]; //5 v.pop_back();
cout << v[1]; //out_of_range exception v.clear();
deque deque
●
#include <deque>
●
sequence container – double-ended queue, dynamic array using non- contiguous storage, a list of arrays
●
fast addition/removal at the beginning and at the end of the sequence:
push_front, pop_front, push_back, pop_back
●
a little slower than for the vector access by indexing operator []
●
a little faster than for the vector insertion/removal at positions other than the beginning or the end: insert, erase
deque<int> d;
d.push_back(1);
d.push_back(3);
d.push_front(2);
cout << d.size(); //3 cout << d[1]; //1 d.pop_front();
list list
●
#include <list>
●
sequence container – double-linked list using non-contiguous storage
●
fast addition/insertion/removal at any position of the sequence: push_front, pop_front, push_back, push_front, insert, erase, remove
●
ordering methods: reverse, sort
●
no indexing operator []
list<int> l;
l.push_back(1);
l.push_back(3);
l.push_front(4);
l.push_front(2); //2 4 1 3 l.reverse(); //3 1 4 2 cout << l.size(); //4
l.sort(); //1 2 3 4
l.remove(3); //1 2 4
stack stack
●
#include <stack>
●
container adapter – adapts the deque container to LIFO structure
●
changing: push, pop
●
reading: top, empty, size
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.top(); //3 cout << s.size(); //3 s.pop();
cout << s.top(); //2 cout << s.size(); //2 if( !s.empty() )
{ … }
queue queue
●
#include <queue>
●
container adapter – adapts the deque container to FIFO structure
●
changing: push, pop
●
reading: front, back, empty, size
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
cout << q.front(); //1 cout << q.back(); //3 cout << q.size(); //3 q.pop();
cout << q.front(); //2 cout << q.size(); //2 if( !q.empty() )
{ … }
priority_queue priority_queue
●
#include <queue>
●
container adapter – adapts the vector container to a heap structure ensuring the ordering of its items from the greatest to the lowest (requires operator<)
●
changing: push, pop
●
reading: top, empty, size
priority_queue<int> q;
q.push(1);
q.push(3);
q.push(2);
cout << q.top(); //3 cout << q.size(); //3 q.pop();
cout << q.top(); //2 cout << q.size(); //2 if( !q.empty() )
{ …
set set
●
#include <set>
●
associative container – stores unique elements in the structure of the binary search tree (unordered_set is a hash table)
●
changing: insert, erase, clear
●
reading: count, empty, size
set<int> s;
s.insert(1);
s.insert(2);
s.insert(1);
cout << s.size(); //2 s.erase(2);
cout << s.size(); //1 cout << s.count(1); //1 cout << s.count(2); //0 if( !s.empty() )
{ … }
map map
●
#include <map>
●
associative container – stores unique keys with mapped values in the structure of the binary search tree (unordered_map is a hash table)
●
changing: [], erase, clear
●
reading: [], count, empty, size
map<int, string> m;
m[1] = "first";
m[2] = "second";
m[3] = "third";
cout << m.size(); //3
cout << m[3]; //third m.erase(2);
cout << m.size(); //2 cout << m.count(1); //1 if( !m.empty() )
{ …