SlideShare a Scribd company logo
C++ STL
(quickest way to learn, even for absolute beginners)
C++ STL is like a weapons-pack or “Toolkit” of C++.
It contains some very useful data structures and algorithms.
STL is one of the reasons why C++ is best for CP.
To start using:
#include<bits/stdc++.h>
using namespace std;
// Now all STL Containers and Functions are ready for use
WATCH MY YOUTUBE
VIDEO FOR DEEP
EXPLANATION OF
THESE SLIDES
About Me
Hi, I am Utkarsh Gupta.
Upcoming Google Software Engineer. (Offcampus, Google contacted me)
I am one of the best Competitive Programmers in India.
Subscribe to my YT Channel for more content
Achievements:
India Ranks 2, 2, 2, 3 in Google Kickstart Round A,B,C,D respectively.
Grandmaster on Codeforces (India Rank 2)
7 star coder on Codechef
Watch me do Leetcode Weekly Contest in less than half time
Not Bragging, just telling it to learners so that they
learn confidently with faith in the teacher.
Benefits of STL
● Using STL, you can write shorter code that runs faster
● The prewritten codes in STL are extremely error-free and optimized.
● As you study advanced concepts - STL will be very important
○ Vector is used for graph adjacency list
○ pairs and sets are used for dijkstra algorithm in graph
○ And many more...
Vector
It is a dynamic sized array. Number of elements can be increased or decreased.
(In Java same behaviour is shown by ArrayList).
vector<int> v; // empty vector of integers
vector<int> v(10); // vector of integers with 10 elements (all 0)
vector<char> v(10,’h’); // vector of chars with 10 elements (all ‘h’)
Important Functions:
v.push_back(x) - insert the value x to the end of the vector. O(1)
v.pop_back() - erase the last element. O(1)
v.clear() - erase all elements. O(n)
v.size() - returns the current size of the vector. O(1)
[] operator - can be used to access elements like an array. O(1)
cout << v[0]; // prints the first element in the vector
Importance of Vector
There are hundreds of use-cases but some of them might be too advanced to
beginners, so here is an easier example.
Given N numbers in input, print 2 lines, in first line, all even integers in sorted
order, in second line, all odd integers in sorted order.
Solution hint:
Make 2 vectors - one for even elements, other for odd elements, push_back() the
elements into the correct vector accordingly. Then sort both vectors and print.
(Note: This problem can be done without vectors also, but it is easier with vectors)
sort()
This function can be used to sort an array or a vector or a string. The underlying
sorting algorithm is called the gcc_sort which is a hybrid algorithm which is
implemented in a very efficient way. O(NlogN)
If you manually write a sorting algorithm, it’ll be slower than this.
Usage:
int a[n];
sort(a,a+n);
vector<int> v;
sort(v.begin(),v.end());
string s;
sort(s.begin(),s.end());
Note: begin() and end() are called iterators, we’ll discuss them later.
In short, they’re a little bit similar to pointers.
Pair
Pair is a way of creating a Composite-Datatype composed of 2 different
primitive/composite datatypes.
pair<int,int> p; // a pair of 2 ints
pair<int,string> p; // a pair of an int and a string
pair<int,pair<int,string>> p; // a pair of int and (pair of int and string)
pair<vector<int>,string> p; // a pair of a (vector of int) and a string
Access elements using .first and .second
pair<string,int> p = {“hello”,6};
cout << p.first << “ “ << p.second; // prints: hello 6
Advantages:
In case, you want to return multiple values from a function.
(see next slide for the main advantage)
Sorting arrays/vectors of Pairs (Very Useful)
Say we have an array of pairs.
pair<int,int> p[5]; // an array of 5 pairs
p[0] = {1,2}; p[1] = {5,2}; p[2] = {8,1}; p[3] = {1,0}; p[4] = {3,4}
Let’s sort this array:
sort(p,p+5);
Now the array looks like:
[{1,0}, {1,2}, {3,4}, {5,2}, {8,1}]
Sorting is done in a way that the ordering is done by the “first” element, but wherever the “first” is equal,
the ties are broken by comparing second.
Try this question:
Given a list of names and scores of students, print the names of students in decreasing order of scores.
Iterators
These behave a lot like pointers.
vector<int> v = {10, 15, 12, 5, 20};
vector<int>::iterator it = v.begin();
// OR
auto it = v.begin();
cout << *it; // 10
it++;
cout << *it; // 15
it--;
cout << *it; // 10
cout << *(it + 3); // 5
int a[5] = {10, 15, 12, 5, 20};
int *p = a;
cout << *p; // 10
p++;
cout << *p; // 15
p--;
cout << *p; // 10
cout << *(p + 3); // 5
“auto” keyword is used to
deduce datatype
automatically
NOTE: v.end() is the iterator to a non-existent
element (after the last element)
Set
Set is a container which keeps a unique copy of every element in sorted order.
(In Java same behaviour is shown by TreeSet).
set<int> s; // empty set of integers
set<string> s; // empty set of strings
Important Functions:
s.insert(x) - insert the value x into set, do nothing if already present. O(log N)
s.erase(x) - erase the value x from set if present. O(log N)
s.count(x) - returns 0 if x is not in set and 1 if x is in set. O(log N)
s.clear() - erase all elements. O(n)
s.size() - returns the current size of the set. O(1)
WRONG: cout << s[0]; // [] operator doesn’t work with set
Set Iterators
Set iterators offer less features than vector iterators.
auto it = s.begin(); // it is the iterator to the first element
it++, it--, ++it, --it -> These are all valid and work in O(logN) time
Functions related to set iterators:
s.find(x): returns iterator to element with value x. Returns s.end() if not found. O(logN)
s.lower_bound(x): returns iterator to the first element which is >= x. Returns s.end() if not found. O(logN)
s.upper_bound(x): returns iterator to the first element which is > x. Returns s.end() if not found. O(logN)
s.erase(it): erases the element with iterator it. O(logN)
Both of the next 2 lines are exactly same.
if(s.find(10) == s.end()) cout << “Not Found”;
if(s.count(10) == 0) cout << “Not Found”;
NOTE: s.end() is the iterator to a non-existent
element (after the last element)
NOTE: (it + 5) or it += 2 etc are INVALID. To advance multiple steps, do it++ multiple times.
Map
You can think of these as special arrays in which the indices(keys) of elements
can be negative or very big or even strings! These are like python-dictionaries. (In
Java same behaviour is shown by TreeMap).
map<key_datatype, value_datatype> m;
map<string, int> m; // defines a map in which the keys of elements are strings
Now we can use it like:
m[“hello”] = 50;
m[“world”] = 12;
cout << m[“hello”] << “ “ << m[“world”]; // 50 12
map<int,int> m;
m[-234] = 49; // negative ints are also valid as keys
Very common use-case: Count frequency of various objects
NOTE: Maps are very similar to
sets, in sets the values are unique
and sorted, in maps, the keys are
unique and sorted
Map (Continued)
m.clear() - Clears a map
m[key] - value of element with key. O(logN)
m.count(key), m.find(key), m.erase(key),
m.lower_bound(key), m.upper_bound(key) - similar to set
Map Iterators behave similar to set iterators, but upon doing *it you instead of
getting the value, you get a pair of {key, value}
Examples:
map<string, double> m;
// insert values in map
auto it = m.find(“utkarsh”);
pair<string, double> p = *it; // {“utkarsh”, m[“utkarsh] }
BONUS:
(*it).first and (*it).second
Can instead be written as
it -> first
it -> second
Iterating Containers
for(auto it = s.begin(); it != s.end(); it++){
// *it
}
This works for all three: set, map and vector
Shorthand:
set<int> s;
for(int x:s){
// x
}
vector<int> v;
for(int x:v){
// x
}
map<int,int> m;
for(pair<int,int> x:v){
// x.first, x.second
}
Try Out These Problems
https://codeforces.com/problemset/problem/22/A (SET)
https://codeforces.com/problemset/problem/782/A (SET)
https://codeforces.com/problemset/problem/4/C (MAP)
https://codeforces.com/contest/903/problem/C (MAP - medium level)
Do these also without knowing which containers to use:
https://www.spoj.com/problems/MINSTOCK/
https://codeforces.com/problemset/problem/799/B
Also, keep practicing problems from Codeforces, div2 B and C often require you to use some STL
containers and functions.
References (Can be used like “Glossary”)
Any STL container or function, you want to learn about: Just google search
“Cplusplus [container name]”
For example: “Cplusplus vector” gives the following result:
https://www.cplusplus.com/reference/vector/vector/
Similarly:
https://www.cplusplus.com/reference/set/set/
https://www.cplusplus.com/reference/map/map/
BEST OF LUCK!
Next Slides are not for
Beginners, they have some
intermediate level stuff,
continue only if you have
good grasp of STL and C++
Custom Comparators (Less Commonly Needed)
You can define your own rule for sorting!
For example:
bool decreasing_order(int x, int y){
return x > y;
}
int a[10];
sort(a, a+10, decreasing_order); // sorts in descending order
The comparator with arguments (x,y) should return true IF AND ONLY IF, x is
necessarily on the left of y in the sorted array. Read more here.
Exercise: Define Custom Comparator to sort pairs in increasing order of first and if
there are ties, break those in decreasing order of second.
NOTE: Using Comparator
Classes, we can apply
custom sorting rules to
sets and maps also
Further Study (Not Relevant for Beginners)
Read about these containers on your own, it should be easy because most of the
important concepts are already covered. These are less commonly used so you
don’t need to worry about these for a long time.
● queue
● stack
● deque
● priority_queue
● multiset / multimap -> can store duplicates (too complex for beginners)
● unordered_set / unordered_map (like HashSet or HashMap in Java)
NOTE: unordered set and map are not-reliable and can perform bad in certain
situations, beginners should always avoid them.

More Related Content

PDF
Python-03| Data types
PPTX
Python Collections
PDF
Python Unit 3 - Control Flow and Functions
DOCX
18 dec pointers and scope resolution operator
PPTX
Types of keys dbms
PPTX
Binary Tree Representation in memory
PPTX
Critical section problem in operating system.
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Python-03| Data types
Python Collections
Python Unit 3 - Control Flow and Functions
18 dec pointers and scope resolution operator
Types of keys dbms
Binary Tree Representation in memory
Critical section problem in operating system.
Data Structures - Lecture 9 [Stack & Queue using Linked List]

What's hot (16)

PPTX
PDF
Method overloading, recursion, passing and returning objects from method, new...
PDF
Set methods in python
PDF
Ggplot2 cheatsheet-2.1
PPT
Chapter 12 ds
PPTX
Switch statement, break statement, go to statement
PDF
Relational algebra in dbms
PDF
Sorting
PPTX
Lecture - 3 Variables-data type_operators_oops concept
PPT
Relational Algebra and Calculus.ppt
PDF
Datatypes in python
PDF
STL in C++
PDF
What is Python.pdf
PPTX
NumPy.pptx
Method overloading, recursion, passing and returning objects from method, new...
Set methods in python
Ggplot2 cheatsheet-2.1
Chapter 12 ds
Switch statement, break statement, go to statement
Relational algebra in dbms
Sorting
Lecture - 3 Variables-data type_operators_oops concept
Relational Algebra and Calculus.ppt
Datatypes in python
STL in C++
What is Python.pdf
NumPy.pptx
Ad

Similar to C++ STL (quickest way to learn, even for absolute beginners).pptx (20)

ODP
Talk on Standard Template Library
PDF
Python - Lecture 12
PPTX
Using-Python-Libraries.9485146.powerpoint.pptx
ODP
C++ STL 概觀
PDF
PPTX
funadamentals of python programming language (right from scratch)
PPTX
The presention is about the queue data structure
PDF
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
PPTX
Object Oriented Programming Using C++: C++ STL Programming.pptx
PPTX
C++11 - A Change in Style - v2.0
PPTX
Introduction to Client-Side Javascript
PDF
Astronomical data analysis by python.pdf
PDF
CE344L-200365-Lab2.pdf
PPTX
Improve Your Edge on Machine Learning - Day 1.pptx
PPT
Array 31.8.2020 updated
DOCX
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
PPTX
The ES Library for JavaScript Developers
PDF
Data Analysis with R (combined slides)
PDF
The Ring programming language version 1.8 book - Part 94 of 202
Talk on Standard Template Library
Python - Lecture 12
Using-Python-Libraries.9485146.powerpoint.pptx
C++ STL 概觀
funadamentals of python programming language (right from scratch)
The presention is about the queue data structure
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Object Oriented Programming Using C++: C++ STL Programming.pptx
C++11 - A Change in Style - v2.0
Introduction to Client-Side Javascript
Astronomical data analysis by python.pdf
CE344L-200365-Lab2.pdf
Improve Your Edge on Machine Learning - Day 1.pptx
Array 31.8.2020 updated
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
The ES Library for JavaScript Developers
Data Analysis with R (combined slides)
The Ring programming language version 1.8 book - Part 94 of 202
Ad

Recently uploaded (20)

PDF
An interstellar mission to test astrophysical black holes
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PDF
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PPTX
2Systematics of Living Organisms t-.pptx
PPTX
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
DOCX
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
PPTX
Science Quipper for lesson in grade 8 Matatag Curriculum
PDF
Lymphatic System MCQs & Practice Quiz – Functions, Organs, Nodes, Ducts
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PPTX
Pharmacology of Autonomic nervous system
PPTX
Classification Systems_TAXONOMY_SCIENCE8.pptx
PPTX
Overview of calcium in human muscles.pptx
PPTX
Application of enzymes in medicine (2).pptx
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
PPT
protein biochemistry.ppt for university classes
PDF
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
An interstellar mission to test astrophysical black holes
POSITIONING IN OPERATION THEATRE ROOM.ppt
Warm, water-depleted rocky exoplanets with surfaceionic liquids: A proposed c...
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
2Systematics of Living Organisms t-.pptx
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
ECG_Course_Presentation د.محمد صقران ppt
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
Science Quipper for lesson in grade 8 Matatag Curriculum
Lymphatic System MCQs & Practice Quiz – Functions, Organs, Nodes, Ducts
7. General Toxicologyfor clinical phrmacy.pptx
Pharmacology of Autonomic nervous system
Classification Systems_TAXONOMY_SCIENCE8.pptx
Overview of calcium in human muscles.pptx
Application of enzymes in medicine (2).pptx
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
protein biochemistry.ppt for university classes
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...

C++ STL (quickest way to learn, even for absolute beginners).pptx

  • 1. C++ STL (quickest way to learn, even for absolute beginners) C++ STL is like a weapons-pack or “Toolkit” of C++. It contains some very useful data structures and algorithms. STL is one of the reasons why C++ is best for CP. To start using: #include<bits/stdc++.h> using namespace std; // Now all STL Containers and Functions are ready for use WATCH MY YOUTUBE VIDEO FOR DEEP EXPLANATION OF THESE SLIDES
  • 2. About Me Hi, I am Utkarsh Gupta. Upcoming Google Software Engineer. (Offcampus, Google contacted me) I am one of the best Competitive Programmers in India. Subscribe to my YT Channel for more content Achievements: India Ranks 2, 2, 2, 3 in Google Kickstart Round A,B,C,D respectively. Grandmaster on Codeforces (India Rank 2) 7 star coder on Codechef Watch me do Leetcode Weekly Contest in less than half time Not Bragging, just telling it to learners so that they learn confidently with faith in the teacher.
  • 3. Benefits of STL ● Using STL, you can write shorter code that runs faster ● The prewritten codes in STL are extremely error-free and optimized. ● As you study advanced concepts - STL will be very important ○ Vector is used for graph adjacency list ○ pairs and sets are used for dijkstra algorithm in graph ○ And many more...
  • 4. Vector It is a dynamic sized array. Number of elements can be increased or decreased. (In Java same behaviour is shown by ArrayList). vector<int> v; // empty vector of integers vector<int> v(10); // vector of integers with 10 elements (all 0) vector<char> v(10,’h’); // vector of chars with 10 elements (all ‘h’) Important Functions: v.push_back(x) - insert the value x to the end of the vector. O(1) v.pop_back() - erase the last element. O(1) v.clear() - erase all elements. O(n) v.size() - returns the current size of the vector. O(1) [] operator - can be used to access elements like an array. O(1) cout << v[0]; // prints the first element in the vector
  • 5. Importance of Vector There are hundreds of use-cases but some of them might be too advanced to beginners, so here is an easier example. Given N numbers in input, print 2 lines, in first line, all even integers in sorted order, in second line, all odd integers in sorted order. Solution hint: Make 2 vectors - one for even elements, other for odd elements, push_back() the elements into the correct vector accordingly. Then sort both vectors and print. (Note: This problem can be done without vectors also, but it is easier with vectors)
  • 6. sort() This function can be used to sort an array or a vector or a string. The underlying sorting algorithm is called the gcc_sort which is a hybrid algorithm which is implemented in a very efficient way. O(NlogN) If you manually write a sorting algorithm, it’ll be slower than this. Usage: int a[n]; sort(a,a+n); vector<int> v; sort(v.begin(),v.end()); string s; sort(s.begin(),s.end()); Note: begin() and end() are called iterators, we’ll discuss them later. In short, they’re a little bit similar to pointers.
  • 7. Pair Pair is a way of creating a Composite-Datatype composed of 2 different primitive/composite datatypes. pair<int,int> p; // a pair of 2 ints pair<int,string> p; // a pair of an int and a string pair<int,pair<int,string>> p; // a pair of int and (pair of int and string) pair<vector<int>,string> p; // a pair of a (vector of int) and a string Access elements using .first and .second pair<string,int> p = {“hello”,6}; cout << p.first << “ “ << p.second; // prints: hello 6 Advantages: In case, you want to return multiple values from a function. (see next slide for the main advantage)
  • 8. Sorting arrays/vectors of Pairs (Very Useful) Say we have an array of pairs. pair<int,int> p[5]; // an array of 5 pairs p[0] = {1,2}; p[1] = {5,2}; p[2] = {8,1}; p[3] = {1,0}; p[4] = {3,4} Let’s sort this array: sort(p,p+5); Now the array looks like: [{1,0}, {1,2}, {3,4}, {5,2}, {8,1}] Sorting is done in a way that the ordering is done by the “first” element, but wherever the “first” is equal, the ties are broken by comparing second. Try this question: Given a list of names and scores of students, print the names of students in decreasing order of scores.
  • 9. Iterators These behave a lot like pointers. vector<int> v = {10, 15, 12, 5, 20}; vector<int>::iterator it = v.begin(); // OR auto it = v.begin(); cout << *it; // 10 it++; cout << *it; // 15 it--; cout << *it; // 10 cout << *(it + 3); // 5 int a[5] = {10, 15, 12, 5, 20}; int *p = a; cout << *p; // 10 p++; cout << *p; // 15 p--; cout << *p; // 10 cout << *(p + 3); // 5 “auto” keyword is used to deduce datatype automatically NOTE: v.end() is the iterator to a non-existent element (after the last element)
  • 10. Set Set is a container which keeps a unique copy of every element in sorted order. (In Java same behaviour is shown by TreeSet). set<int> s; // empty set of integers set<string> s; // empty set of strings Important Functions: s.insert(x) - insert the value x into set, do nothing if already present. O(log N) s.erase(x) - erase the value x from set if present. O(log N) s.count(x) - returns 0 if x is not in set and 1 if x is in set. O(log N) s.clear() - erase all elements. O(n) s.size() - returns the current size of the set. O(1) WRONG: cout << s[0]; // [] operator doesn’t work with set
  • 11. Set Iterators Set iterators offer less features than vector iterators. auto it = s.begin(); // it is the iterator to the first element it++, it--, ++it, --it -> These are all valid and work in O(logN) time Functions related to set iterators: s.find(x): returns iterator to element with value x. Returns s.end() if not found. O(logN) s.lower_bound(x): returns iterator to the first element which is >= x. Returns s.end() if not found. O(logN) s.upper_bound(x): returns iterator to the first element which is > x. Returns s.end() if not found. O(logN) s.erase(it): erases the element with iterator it. O(logN) Both of the next 2 lines are exactly same. if(s.find(10) == s.end()) cout << “Not Found”; if(s.count(10) == 0) cout << “Not Found”; NOTE: s.end() is the iterator to a non-existent element (after the last element) NOTE: (it + 5) or it += 2 etc are INVALID. To advance multiple steps, do it++ multiple times.
  • 12. Map You can think of these as special arrays in which the indices(keys) of elements can be negative or very big or even strings! These are like python-dictionaries. (In Java same behaviour is shown by TreeMap). map<key_datatype, value_datatype> m; map<string, int> m; // defines a map in which the keys of elements are strings Now we can use it like: m[“hello”] = 50; m[“world”] = 12; cout << m[“hello”] << “ “ << m[“world”]; // 50 12 map<int,int> m; m[-234] = 49; // negative ints are also valid as keys Very common use-case: Count frequency of various objects NOTE: Maps are very similar to sets, in sets the values are unique and sorted, in maps, the keys are unique and sorted
  • 13. Map (Continued) m.clear() - Clears a map m[key] - value of element with key. O(logN) m.count(key), m.find(key), m.erase(key), m.lower_bound(key), m.upper_bound(key) - similar to set Map Iterators behave similar to set iterators, but upon doing *it you instead of getting the value, you get a pair of {key, value} Examples: map<string, double> m; // insert values in map auto it = m.find(“utkarsh”); pair<string, double> p = *it; // {“utkarsh”, m[“utkarsh] } BONUS: (*it).first and (*it).second Can instead be written as it -> first it -> second
  • 14. Iterating Containers for(auto it = s.begin(); it != s.end(); it++){ // *it } This works for all three: set, map and vector Shorthand: set<int> s; for(int x:s){ // x } vector<int> v; for(int x:v){ // x } map<int,int> m; for(pair<int,int> x:v){ // x.first, x.second }
  • 15. Try Out These Problems https://codeforces.com/problemset/problem/22/A (SET) https://codeforces.com/problemset/problem/782/A (SET) https://codeforces.com/problemset/problem/4/C (MAP) https://codeforces.com/contest/903/problem/C (MAP - medium level) Do these also without knowing which containers to use: https://www.spoj.com/problems/MINSTOCK/ https://codeforces.com/problemset/problem/799/B Also, keep practicing problems from Codeforces, div2 B and C often require you to use some STL containers and functions.
  • 16. References (Can be used like “Glossary”) Any STL container or function, you want to learn about: Just google search “Cplusplus [container name]” For example: “Cplusplus vector” gives the following result: https://www.cplusplus.com/reference/vector/vector/ Similarly: https://www.cplusplus.com/reference/set/set/ https://www.cplusplus.com/reference/map/map/ BEST OF LUCK!
  • 17. Next Slides are not for Beginners, they have some intermediate level stuff, continue only if you have good grasp of STL and C++
  • 18. Custom Comparators (Less Commonly Needed) You can define your own rule for sorting! For example: bool decreasing_order(int x, int y){ return x > y; } int a[10]; sort(a, a+10, decreasing_order); // sorts in descending order The comparator with arguments (x,y) should return true IF AND ONLY IF, x is necessarily on the left of y in the sorted array. Read more here. Exercise: Define Custom Comparator to sort pairs in increasing order of first and if there are ties, break those in decreasing order of second. NOTE: Using Comparator Classes, we can apply custom sorting rules to sets and maps also
  • 19. Further Study (Not Relevant for Beginners) Read about these containers on your own, it should be easy because most of the important concepts are already covered. These are less commonly used so you don’t need to worry about these for a long time. ● queue ● stack ● deque ● priority_queue ● multiset / multimap -> can store duplicates (too complex for beginners) ● unordered_set / unordered_map (like HashSet or HashMap in Java) NOTE: unordered set and map are not-reliable and can perform bad in certain situations, beginners should always avoid them.