Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Lecture11 standard template-library

574
views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
574
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
18
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 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, reusablecomponents that implement many common datastructures and algorithms used to process those datastructures.The STL was conceived and designed forperformance and flexibility. TCP1201 OOPDS 3
• 4. STL ComponentsSTL has 3 components (CIA):Containers, Algorithms, Iterators.Containers: Containers are data structures or acollection of objects. Example:vector, list, set, map, etc.Iterators: Iterators are similar to pointers and are usedto point to container elements.Algorithms: Algorithm are functions for processingcontainer elements. Example: copy, sort, find, etc. TCP1201 OOPDS 4
• 5. STL ContainersSTL 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 thattypically can locate (search) elements stored in thecontainers quickly, e.g. set, multiset, map,multimap.Container adapters are simply variations of the abovecontainers, e.g. stack, queue, priority_queue. Thecontainer adapters do not support iterators. TCP1201 OOPDS 5
• 6. Useful Sequential Containers Container Descriptionlist 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 Descriptionset 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 Descriptionstack 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 IteratorIterators are similar to pointers and are used to point tocontainer elements.The dereferencing operator (*) dereferences an iteratorso that you can use the element to which it points.The ++ operation on an iterator moves it to thecontainer’s next element.Containers begin method returns an iterator pointing tothe first element of the container.Containers end method returns an iterator pointing tothe first element past the end of the container (anelement that doesn’t exist). TCP1201 OOPDS 9
• 10. Iterator Example#include <iostream> Output:#include <vector> 4 2 7 6using namespace std; 4 2 7 6int 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 AlgorithmsMany STL algorithms accept iterator as functionargument. See next slide for sort function.Many STL algorithms returns an iterator. See theexample 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 ClassA 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 needrandom 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[] isnot 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> -999using namespace std; 18int 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
• 15. STL multiset Class#include <iostream> Output:#include <set> -999using namespace std; -999int main() { 18 multiset<int> s; 321 s.insert (321); s.insert (-999); s.insert (18); s.insert (-999); // duplicate set<int>::iterator it = s.begin(); while (it != s.end()) multiset allows cout << *it++ << endl; duplicate keys} TCP1201 OOPDS 15
• 16. STL map ClassA 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 andthe value respectively.In array the index is always int starting from 0, whereas inmap 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 ClassA multimap is similar to map but it allows duplicate keys.However, insert method and a pair object must be usedwhen inserting a (key,value) pair into multimap.The pair object and the multimap must have the same keytype and value type.Operator [ ] is not supported. Iterator must be used to locate aelement. 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 ObjectsBy default, STL sort function, set, map, and otherclasses use the STL less function object to sort theelements from the smallest element first to the largest.To sort by the largest element first, pass the STLgreater function object to the STL containerconstructor. 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> 321using namespace std; 18int 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