SlideShare a Scribd company logo
Lecture 11




  Standard Template Library
            (STL)




TCP1201 OOPDS                 1
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
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
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
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
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
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
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
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
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
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
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
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
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
STL multiset Class
#include <iostream>                    Output:
#include <set>                         -999
using namespace std;                   -999
int 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
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
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
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
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
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
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
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

More Related Content

PPTX
Lecture05 operator overloading-and_exception_handling
PPTX
Lecture02 class -_templatev2
PPTX
Lecture08 stacks and-queues_v3
PDF
An Introduction to Part of C++ STL
PDF
C++ Standard Template Library
PPTX
Lecture07 the linked-list_as_a_data_structure_v3
PPTX
Standard Template Library
ODP
C++ STL 概觀
Lecture05 operator overloading-and_exception_handling
Lecture02 class -_templatev2
Lecture08 stacks and-queues_v3
An Introduction to Part of C++ STL
C++ Standard Template Library
Lecture07 the linked-list_as_a_data_structure_v3
Standard Template Library
C++ STL 概觀

What's hot (20)

PPT
standard template library(STL) in C++
PPT
Stl Containers
PPT
Stl (standard template library)
PDF
STL in C++
PPTX
Lecture06 methods for-making_data_structures_v2
PDF
Java Collections API
PPT
Collections Framework
PPTX
Java Generics
PPT
Java Generics for Dummies
PDF
Java 8 Stream API. A different way to process collections.
PDF
Java Generics Introduction - Syntax Advantages and Pitfalls
PPTX
Generics in .NET, C++ and Java
PDF
Arrays in C++
PDF
On Parameterised Types and Java Generics
PPT
Core java by a introduction sandesh sharma
PPTX
Scala Back to Basics: Type Classes
ODP
Functional Programming With Scala
PPT
Arrays in C++
PPTX
Vector class in C++
standard template library(STL) in C++
Stl Containers
Stl (standard template library)
STL in C++
Lecture06 methods for-making_data_structures_v2
Java Collections API
Collections Framework
Java Generics
Java Generics for Dummies
Java 8 Stream API. A different way to process collections.
Java Generics Introduction - Syntax Advantages and Pitfalls
Generics in .NET, C++ and Java
Arrays in C++
On Parameterised Types and Java Generics
Core java by a introduction sandesh sharma
Scala Back to Basics: Type Classes
Functional Programming With Scala
Arrays in C++
Vector class in C++
Ad

Similar to Lecture11 standard template-library (20)

PPTX
C++11 - STL Additions
PPT
Lecture#5-Arrays-oral patholohu hfFoP.ppt
PPTX
Object Oriented Programming Using C++: C++ STL Programming.pptx
PPTX
Lecture 9_Classes.pptx
PDF
Sppu University|BE|Computer Engineering|OOPs|unit 6 notes|ppt
PPT
Computer Programming- Lecture 8
PDF
C++ Nested loops, matrix and fuctions.pdf
PPTX
The presention is about the queue data structure
PPTX
C++ STL (quickest way to learn, even for absolute beginners).pptx
PPTX
C++ STL (quickest way to learn, even for absolute beginners).pptx
PDF
Object Oriented Programming (OOP) using C++ - Lecture 5
PDF
Using CUDA Within Mathematica
PDF
Using Cuda Within Mathematica
PDF
STLStack.pdf
PPTX
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
PPTX
Arrays in C++
PDF
The STL
PPT
Lecture#8 introduction to array with examples c++
PDF
Arrays and strings in c++
PPT
Lecture#9 Arrays in c++
C++11 - STL Additions
Lecture#5-Arrays-oral patholohu hfFoP.ppt
Object Oriented Programming Using C++: C++ STL Programming.pptx
Lecture 9_Classes.pptx
Sppu University|BE|Computer Engineering|OOPs|unit 6 notes|ppt
Computer Programming- Lecture 8
C++ Nested loops, matrix and fuctions.pdf
The presention is about the queue data structure
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Object Oriented Programming (OOP) using C++ - Lecture 5
Using CUDA Within Mathematica
Using Cuda Within Mathematica
STLStack.pdf
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Arrays in C++
The STL
Lecture#8 introduction to array with examples c++
Arrays and strings in c++
Lecture#9 Arrays in c++
Ad

More from Hariz Mustafa (20)

PPTX
Lecture10 trees v3
PPTX
Lecture09 recursion
PPTX
Lecture04 polymorphism
PPTX
Lecture03 inheritance
PPTX
Lecture01 object oriented-programming
DOC
Topic6decisionmaking
DOC
Topic5cognition and problem_solving
DOC
Topic2 argument
PPT
PPT
Topic 1
PPT
Problem solving activities
DOC
Exercise answers chapter 1, 2 & 3
PPT
Decision making scenarios
PPT
Decision making
PPT
Cognition and problem_solving
DOC
Chapter 6 logical_fallacies_ii
DOC
Chapter 4 language
PPT
Ch08 evaluating arguments
DOC
Chapter 5 logical_fallacies_i
PPT
Ch03 basic logical_concepts
Lecture10 trees v3
Lecture09 recursion
Lecture04 polymorphism
Lecture03 inheritance
Lecture01 object oriented-programming
Topic6decisionmaking
Topic5cognition and problem_solving
Topic2 argument
Topic 1
Problem solving activities
Exercise answers chapter 1, 2 & 3
Decision making scenarios
Decision making
Cognition and problem_solving
Chapter 6 logical_fallacies_ii
Chapter 4 language
Ch08 evaluating arguments
Chapter 5 logical_fallacies_i
Ch03 basic logical_concepts

Lecture11 standard template-library

  • 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
  • 15. STL multiset Class #include <iostream> Output: #include <set> -999 using namespace std; -999 int 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 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