C++ STL
C++ STL
(n,0));
The above line means that a 2D vector of m rows and n columns with each element
being 0 is created
To declare a nested vector of size 2 but the inside vector shoudld support
push_back, declare as - vector<vector<int>> dp(2,vector<int>());
s.substr(3,2) - for s="geeks", the returned value is "ks" (starts from 0-index 3
and has length 2).
sorting for nested vectors based on the first or second value of the nested vector
can be done by:
bool cmp(vector<int>& a,vector<int>& b) {return a[1]<b[1];} - this is for
sorting into ascending order on basis of the 2nd value of the nested vector
sort(indices.begin(), indices.end(),cmp);
PAIR:
pair<type1,type2> p;
type1 values accessed as p.first
type2 values accessed as p.second
VECTOR:
begin()
end()
resize()
empty()
front()
back()
push_back()
pop_back()
emplace() - extends container by inserting new element at the position
emplace_back() - inserts new element at the end after extending the vector
container
QUEUE:
empty()
emplace()
front()
back()
push()
pop() - deletes first element of queue
STACK:
empty()
top()
push()
pop() - deletes last element of stack
empty()
top()
push()
pop()
emplace()
MAP: (the key values are sorted in ascending order by default) - also has
unordered_map
SET: (the values are sorted in ascending order by default) - also has unordered_set
(can also act as min and max heap with random element deletion)
s.insert()
s.erase(value)
begin() - min value [values are accessed as *s.begin()]
rbegin() - max value [values are accessed as *s.rbegin()]
end()
empty()
Traverse it as - for (auto i: s) cout<<i<<'\n';
MULTISET (the same as SET but can also store duplicate values):
If there are two 3 in the multiset and to delete just a single 3, do
auto it=s.find(3);
s.erase(it)
STRING:
swap(s[idx1],s[idx2]);
pop_back();
s.erase(1) - deletes all characters except the first one (not 1st index)
s.erase(1,4) - deletes 4 characters from index 1 (including index 1)
s.substr(3,2) - returns a substring from index 3, of length 2 (including
index 3)
front()
back()
push_front()
push_back()
pop_front()
pop_back()
empty()
insert()
erase()
Bucket sort:
Declare a nested vector like vector<vector<int>> bucket();
Store the frequencies into the index of the bucket and the values of the
bucket[i] should hold the integers that have that frequency.
Frequency corresponds to the index of bucket.
Counting sort:
It can be used when the number of elements or range is small.
For example, it can be used to sort strings as they only have 26 letters at
most.
To do so, create a vector<int> and tabulate the frequency of characters of
the string.
Iterate from index 0 to 26 and build the string.
The resulting string would be sorted.
Backtracking:
To solve backtracking problems, just do DFS.
Write a function dfs that is called recursively within itself to explore all
possible states.
If any present state matches the required condition, update the answer.
Don't forget to pop_back() the dummy variable through which the values are
tracked as you head to the next state.