C++ STL (Quickest Way To Learn, Even For Absolute Beginners)
C++ STL (Quickest Way To Learn, Even For Absolute Beginners)
C++ STL
LANATION OF THESE S
LIDES
To start using:
#include<bits/stdc++.h>
using namespace std;
// Now all STL Containers and Functions are ready for use
Not Bragging, just telling it to learners so that they
About Me learn confidently with faith in the teacher.
Achievements:
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)
Usage:
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
map<int,int> m;
m[-234] = 49; // negative ints are also valid as keys
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}
BONUS:
(*it).first and (*it).second
Can instead be written as
Examples: it -> first
map<string, double> m; it -> second
Shorthand:
vector<int> v; set<int> s; map<int,int> m;
// x // x // x.first, x.second
} } }
Try Out These Problems
https://codeforces.com/problemsext/problem/22/A (SET)
https://codeforces.com/problemset/problem/782/A (SET)
https://codeforces.com/problemset/problem/4/C (MAP)
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]”
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: NOTE: Using
Comparator Classes, we
bool decreasing_order(int x, int y){ can apply custom sorting
rules to sets and maps
return x > y; also
}
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
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.