Programming and Data Structures Programming and Data Structures
Lecture 12 Lecture 12
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
Iterators Iterators
● iterators are generalized pointers to the items of containers
● iterators (like pointers) can be initialised (begin, rbegin), incremented (++), decremented (--), dereferenced (*, ->), compared (==, !=) or tested whether they are empty (end, rend)
● iterators are similar for all the containers, thus any change of the container used is transparent for existing code
vector<int> v = {1, 2, 3, 4, 5};
vector<int>::iterator i; //iterator
for(i = v.begin(); i != v.end(); i++) //explicit use cout << (*i) << endl;
for(i = v.rbegin(); i != v.rend(); i--) //explicit reverse use cout << (*i) << endl;
for(auto a: v) //implicit use cout << a << endl;
Iterators Iterators
● some methods of sequence containers require iterators, e.g. insert and erase operations for vectors, deques and lists
● some methods of associative containers return iterators, e.g. find operation for sets and maps
list<int> l = {1, 2, 3, 4, 5};
l.insert(l.begin() + 1, 7); //1 7 2 3 4 5 l.erase(l.end() - 1); //1 7 2 3 4
map<int, string> m = {{1,"aaa"}, {2,"bbb"}, {3,"ccc"}};
map<int, string>::iterator i = m.find(2);
if (i == m.end())
cout << "not found";
else cout << i->first << ":" << i->second; //2:bbb
Generalized sorting Generalized sorting
● #include <algorithm>
● sort function sorts in the ascending order the contents of a container (between the starting and the ending iterator)
● container items must support operator<
list<int> l;
for(int i = 0; i < 10; i++)
l.push_back(rand() % 100); //random values sort(l.begin(), l.end()) //in-place sorting
Generalized copying Generalized copying
● #include <algorithm>
● copy function copies a cantainer data to another container using the starting and ending iterators of the source container, and the destination iterator
● the destination container should be preallocated
vector<int> v1, v2(10);
for(int i = 0; i < 10; i++)
v1.push_back(rand() % 100); //random values
copy(v1.begin(), v1.end(), v2.begin()); //v1 → v2
Insertion iterators Insertion iterators
● #include <iterator>
● template iterators to fill cantainers (front_insert_iterator, back_insert_iterator) without preallocation
● template functions generating the iterators: front_inserter, back_inserter
vector<int> v1, v2;
for(int i = 0; i < 10; i++)
v1.push_back(rand() % 100); //random values
back_insert_iterator< vector<int> > i(v2); //iterator copy(v1.begin(), v1.end(), i); //v1 → v2
//alterative form:
copy(v1.begin(), v1.end(), back_inserter(v2)); //v1 → v2
Stream iterators Stream iterators
● #include <iterator>
● template iterators to copy data to/from streams (istream_iterator, ostream_iterator)
vector<int> v;
for(int i = 0; i < 10; i++)
v.push_back(rand() % 100); //random values
ostream_iterator<int> out(cout, ","); //stream iterator copy(v.begin(), v.end(), out); //v → cout