1. Lecture 11
Standard Template Library
(STL)
TCP1201 OOPDS 1
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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
10. Iterator Example
#include <iostream> Output:
#include <vector> 4 2 7 6
using namespace std;
4 2 7 6
int main() {
vector<int> v;
v.push_back(4); Iterator type must
v.push_back(2);
v.push_back(7); match container type.
v.push_back(6);
for (int i = 0; i < v.size(); i++) Initialize iterator it to
cout << v[i] << " ";
cout << endl; the first element of
container.
// Same result as 'for' loop.
for (vector<int>::iterator it = v.begin();
it != v.end();
it++) Move to the next
cout << *it << " "; element.
cout << endl;
}
Use iterator it like a
pointer.
TCP1201 OOPDS 10
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 11
12. STL Algorithm sort
#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); vector elements can
v.push_back(6); be sorted using STL
cout << "Vector elements unsorted:n"; algorithm sort
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
function
sort (v.begin(), v.end());
cout << "nVector elements sorted:n"; Vector elements unsorted:
for (int i = 0; i < v.size(); i++) 4 2 7 6
cout << v[i] << " "; Vector elements sorted:
} 2 4 6 7
TCP1201 OOPDS 12
13. STL set Class
A set is a collection of non-duplicate sorted elements called keys.
set <key_type> s;
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:
set <int> s;
s.insert (321);
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 .
TCP1201 OOPDS 13
14. STL set Class
#include <iostream> Output1:
#include <set> -999
using namespace std; 18
int main() { 321
set<int> s; Enter an integer:
s.insert (321); 5
s.insert (-999); 5 is not in set.
s.insert (18);
s.insert (-999); // duplicate is ignored
set<int>::iterator it = s.begin();
while (it != s.end()) Use iterator to
cout << *it++ << endl; // -999 18 321 iterate the set.
int target;
cout << "Enter an integer: "; Output2:
cin >> target; -999
it = s.find (target); 18
if (it == s.end()) // not found 321
cout << target << " is NOT in set."; Enter an integer:
else 321
cout << target << " is IN set."; 321 is IN set.
}
TCP1201 OOPDS 14
16. STL map Class
A map is a collection of (key,value) pairs sorted by the keys.
map <key_type, value_type> m;
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).
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 16
17. STL map Class
#include <iostream> char key;
#include <string> cout << "Enter a char: ";
#include <map> // map, multimap cin >> key;
using namespace std; it = m.find (key);
int main() { if (it == m.end())
map <char, string> m; cout << key
m['C'] = "Cat"; // insert << " is NOT in map.";
m['A'] = "Apple"; else
m['B'] = "Boy"; cout << key << " is IN map.";
cout << m['A'] << " " // retrieve }
<< m['B'] << " "
<< m['C'] << endl;
map <char, string>::iterator it;
it = m.begin(); first refers to the key of
while (it != m.end()) {
cout << it->first << " " current element whereas
<< it->second << endl; second refers to the value
it++; of of current element
}
TCP1201 OOPDS 17
18. STL map Class
#include <iostream> char key;
#include <string> cout << "Enter a char: ";
#include <map> // map, multimap cin >> key;
using namespace std; it = m.find (key);
int main() { if (it == m.end())
map <char, string> m; cout << key
m['C'] = "Cat"; // insert << " is NOT in map.";
m['A'] = "Apple"; else
m['B'] = "Boy"; cout << key << " is IN map.";
cout << m['A'] << " " // retrieve }
<< m['B'] << " "
<< m['C'] << endl;
Output 1: Output 2:
map <char, string>::iterator it; Apple Boy Cat Apple Boy Cat
it = m.begin(); A Apple A Apple
while (it != m.end()) { B Boy B Boy
cout << it->first << " " C Cat C Cat
<< it->second << endl; Enter a char: Z Enter a char: C
it++; Z is NOT in map C is IN map
}
TCP1201 OOPDS 18
19. STL multimap Class
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.
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 19
20. STL multimap Class
#include <iostream> char key;
#include <string> cout << "Enter a char: ";
#include <map> // map, multimap cin >> key;
using namespace std; it = mm.find (key);
int main() { if (it == mm.end())
multimap <char,string> mm; cout << key
mm.insert ( << " is NOT in map.";
pair<char,string>('C',"Cat")); else
mm.insert ( cout << key << " is IN map.";
pair<char,string>('A',"Apple")); }
mm.insert (
pair<char,string>('B',"Boy"));
mm.insert ( Output 1: Output 2:
pair<char,string>('A',"Angle")); A Apple A Apple
A Angle A Angle
map <char, string>::iterator it; B Boy B Boy
it = mm.begin(); C Cat C Cat
while (it != mm.end()) { Enter a char: Z Enter a char: C
cout << it->first << " " Z is NOT in map C is IN map
<< it->second << endl;
it++;
}
TCP1201 OOPDS 20
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 21
22. STL greater Function Object
#include <iostream> Output:
#include <set> 321
using namespace std; 18
int main() { -999
multiset<int, greater<int> > s; -999
s.insert (321);
s.insert (-999);
s.insert (18);
s.insert (-999); // duplicate
set<int>::iterator it = s.begin();
while (it != s.end()) Largest first
cout << *it++ << endl;
}
TCP1201 OOPDS 22