lecture11 standard template-library
DESCRIPTION
TRANSCRIPT
TCP1201 OOPDS 2
Learning Objectives
To understand the components of Standard Template Library (STL).
To understand how and when to use STL’s vector, set, multiset, map, and multimap.
To understand how to use iterator. To understand how to use STL find and
sort algorithms.
TCP1201 OOPDS 3
Standard Template Library (STL)
STL provides powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures.
The STL was conceived and designed for performance and flexibility.
TCP1201 OOPDS 4
STL Components
STL has 3 components (CIA): Containers, Algorithms, Iterators.
Containers: Containers are data structures or a collection of objects. Example: vector, list, set, map, etc.
Iterators: Iterators are similar to pointers and are used to point to container elements.
Algorithms: Algorithm are functions for processing container elements. Example: copy, sort, find, etc.
TCP1201 OOPDS 5
STL Containers
STL has many containers, divided into 3 groups: sequential, associative, adaptor.
Sequential containers represent linear data structures, e.g. vectors, list, deque.
Associative containers are nonlinear containers that typically can locate (search) elements stored in the containers quickly, e.g. set, multiset, map, multimap.
Container adapters are simply variations of the above containers, e.g. stack, queue, priority_queue. The container adapters do not support iterators.
TCP1201 OOPDS 6
Useful Sequential Containers
Container Description
list Bidirectional/Doubly linked list.Best for rapid insertion and deletion anywhere.
vector "Array" that grows automatically, Best for rapid insertion and deletion at back.Support direct access to any elementvia operator "[]".
deque "Array" that grows automatically.Best for rapid insertion and deletion at front and back.
TCP1201 OOPDS 7
Useful Associative Containers
Container Description
set No duplicate element allowed.Elements are automatically sorted.Best for rapid lookup (searching) of element.
multiset set that allows duplicate elements.
map Collection of (key, value) pairs with non-duplicate key.Elements are automatically sorted by key.Best for rapid lookup of key.
multimap map that allows duplicate keys.
TCP1201 OOPDS 8
Useful Container Adaptors
Container Description
stack Last-in, first-out (LIFO) data structure.
queue First-in, first-out (FIFO) data structure.
priority_queue Highest priority element is always the first element out.
TCP1201 OOPDS 9
STL Iterator
Iterators are similar to pointers and are used to point to container elements.
The dereferencing operator (*) dereferences an iterator so that you can use the element to which it points.
The ++ operation on an iterator moves it to the container’s next element.
Container's begin method returns an iterator pointing to the first element of the container.
Container's end method returns an iterator pointing to the first element past the end of the container (an element that doesn’t exist).
TCP1201 OOPDS 1010
#include <iostream>#include <vector>using namespace std;int main() { vector<int> v; v.push_back(4); v.push_back(2); v.push_back(7); v.push_back(6);
for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl;
// Same result as 'for' loop. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) cout << *it << " "; cout << endl;}
Output:4 2 7 64 2 7 6
Iterator type must match container type.
Initialize iterator it to the first element of container.
Move to the next element.
Use iterator it like a pointer.
Iterator Example
TCP1201 OOPDS 11
STL Algorithms
Many STL algorithms accept iterator as function argument. See next slide for sort function.
Many STL algorithms returns an iterator. See the example on STL set class for find function.
TCP1201 OOPDS 1212
#include <iostream>#include <algorithm> // sort()#include <vector>using namespace std;int main() { vector<int> v; v.push_back(4); v.push_back(2); v.push_back(7); v.push_back(6); cout << "Vector elements unsorted:\n"; for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
sort (v.begin(), v.end()); cout << "\nVector elements sorted:\n"; for (int i = 0; i < v.size(); i++) cout << v[i] << " ";}
Vector elements unsorted:4 2 7 6Vector elements sorted:2 4 6 7
vector elements can be sorted using STL algorithm sort function
STL Algorithm sort
TCP1201 OOPDS 13
A set is a collection of non-duplicate sorted elements called keys.
key_type is the data types of the key/element.
Use set when you want a sorted collection and you do not need random access to its elements.
Use insert() method to insert an element into a set:
Duplicates are ignored when inserted.
iterator is required to iterate/visit the elements in set. Operator[] is not supported.
Use find() method to look up a specified key in a set .
STL set Class
set <key_type> s;
set <int> s;s.insert (321);
TCP1201 OOPDS 14
#include <iostream>#include <set>using namespace std;int main() { set<int> s; s.insert (321); s.insert (-999); s.insert (18); s.insert (-999); // duplicate is ignored set<int>::iterator it = s.begin(); while (it != s.end()) cout << *it++ << endl; // -999 18 321 int target; cout << "Enter an integer: "; cin >> target; it = s.find (target); if (it == s.end()) // not found cout << target << " is NOT in set."; else cout << target << " is IN set.";}
Output1:-99918321Enter an integer: 55 is not in set.
Use iterator to iterate the set.
Output2:-99918321Enter an integer: 321321 is IN set.
STL set Class
TCP1201 OOPDS 15
#include <iostream>#include <set>using namespace std;int main() { multiset<int> s; s.insert (321); s.insert (-999); s.insert (18); s.insert (-999); // duplicate set<int>::iterator it = s.begin(); while (it != s.end()) cout << *it++ << endl; }
Output:-999-99918321
STL multiset Class
multiset allows duplicate keys
TCP1201 OOPDS 16
A map is a collection of (key,value) pairs sorted by the keys.
key_type and value_type are the data types of the key and the value respectively.
In array the index is always int starting from 0, whereas in map the key can be of other data type. map cannot contain duplicate key (multimap can).
STL map Class
map <key_type, value_type> m;
map <char, string> m; m['A'] = "Apple"; m['A'] = "Angel"; // key 'A' already in the // map, new 'A' is ignored. // m['A'] is still "Apple".
TCP1201 OOPDS 17
#include <iostream>#include <string>#include <map> // map, multimapusing namespace std;int main() { map <char, string> m; m['C'] = "Cat"; // insert m['A'] = "Apple"; m['B'] = "Boy"; cout << m['A'] << " " // retrieve << m['B'] << " " << m['C'] << endl;
map <char, string>::iterator it; it = m.begin(); while (it != m.end()) { cout << it->first << " " << it->second << endl; it++; }
char key; cout << "Enter a char: "; cin >> key; it = m.find (key); if (it == m.end()) cout << key << " is NOT in map."; else cout << key << " is IN map.";}
STL map Class
first refers to the key of current element whereas second refers to the value of of current element
TCP1201 OOPDS 18
#include <iostream>#include <string>#include <map> // map, multimapusing namespace std;int main() { map <char, string> m; m['C'] = "Cat"; // insert m['A'] = "Apple"; m['B'] = "Boy"; cout << m['A'] << " " // retrieve << m['B'] << " " << m['C'] << endl;
map <char, string>::iterator it; it = m.begin(); while (it != m.end()) { cout << it->first << " " << it->second << endl; it++; }
Output 1:Apple Boy CatA AppleB BoyC CatEnter a char: ZZ is NOT in map
char key; cout << "Enter a char: "; cin >> key; it = m.find (key); if (it == m.end()) cout << key << " is NOT in map."; else cout << key << " is IN map.";}
STL map Class
Output 2:Apple Boy CatA AppleB BoyC CatEnter a char: CC is IN map
TCP1201 OOPDS 19
A multimap is similar to map but it allows duplicate keys.
However, insert method and a pair object must be used when inserting a (key,value) pair into multimap.
The pair object and the multimap must have the same key type and value type.
Operator [ ] is not supported. Iterator must be used to locate a element.
STL multimap Class
multimap <char,string> mm; mm.insert (pair<char,string>('A',"Apple")); mm.insert (pair<char,string>('A',"Angel")); // mm has 2 elements with 'A' as key.
TCP1201 OOPDS 20
#include <iostream>#include <string>#include <map> // map, multimapusing namespace std;int main() { multimap <char,string> mm; mm.insert ( pair<char,string>('C',"Cat")); mm.insert
( pair<char,string>('A',"Apple"));
mm.insert ( pair<char,string>('B',"Boy")); mm.insert
( pair<char,string>('A',"Angle"));
map <char, string>::iterator it; it = mm.begin(); while (it != mm.end()) { cout << it->first << " " << it->second << endl; it++; }
Output 1:A AppleA AngleB BoyC CatEnter a char: ZZ is NOT in map
char key; cout << "Enter a char: "; cin >> key; it = mm.find (key); if (it == mm.end()) cout << key << " is NOT in map."; else cout << key << " is IN map.";}
STL multimap Class
Output 2:A AppleA AngleB BoyC CatEnter a char: CC is IN map
TCP1201 OOPDS 21
STL less and greater Function Objects
By default, STL sort function, set, map, and other classes use the STL less function object to sort the elements from the smallest element first to the largest.
To sort by the largest element first, pass the STL greater function object to the STL container constructor.
set <int> s1; // Smallest to largest. set <int, less<int> > s2; // Same sorting as s1. set <int, greater<int> > s3; // Largest to smallest.
TCP1201 OOPDS 22
#include <iostream>#include <set>using namespace std;int main() { multiset<int, greater<int> > s; s.insert (321); s.insert (-999); s.insert (18); s.insert (-999); // duplicate set<int>::iterator it = s.begin(); while (it != s.end()) cout << *it++ << endl; }
Output:32118-999-999
STL greater Function Object
Largest first