0% found this document useful (0 votes)
45 views4 pages

C++ STL

The document provides information on various C++ data structures and algorithms including: 1) How to declare and initialize nested vectors, find occurrences of values in containers using count(), and convert between strings and integers. 2) Details on sorting nested vectors, lower_bound, upper_bound, pairs, vectors, queues, stacks, priority queues, deques, maps, sets, multisets, and strings. 3) Overviews of bucket sort, counting sort, backtracking, and returning a subset of elements from a container.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

C++ STL

The document provides information on various C++ data structures and algorithms including: 1) How to declare and initialize nested vectors, find occurrences of values in containers using count(), and convert between strings and integers. 2) Details on sorting nested vectors, lower_bound, upper_bound, pairs, vectors, queues, stacks, priority queues, deques, maps, sets, multisets, and strings. 3) Overviews of bucket sort, counting sort, backtracking, and returning a subset of elements from a container.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Nested vector of some size can be declared as: vector<vector<int>> dp(m,vector<int>

(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>());

find() and count() can be used for any data structure

While passing a function, if possible declare it with & as int func(vector<int>&


a,vector<int>& b); as that makes the program run much faster

To use min() and max() for more than 2 values, do min({a,b,c,d,......});

To find the number of occurences of a certain character or integer -


cnt=count(arr.begin(),arr.end(),character(for eg. '1' or 'e' or 2 or 1));

stoi() - coverts string to integer


to_string() - converts integer to string

s.substr(3,2) - for s="geeks", the returned value is "ks" (starts from 0-index 3
and has length 2).

count(nums.begin(),nums.end(),1 or 'a' or ' ' or some character or number); -


return long long. So, be careful while using inside min() or max()

__builtin_popcount(x) - returns numbers of 1 in binary representation of x

__gcd(x,y) - returns the gcd of x and y

accumulate(nums.begin(),nums.end(),5); - this sums up the entire array and adds 5


to the sum and returns it
accumulate return the data type of value passed(5 in the above line which is int),
not the data type assigned to accumulate. Use accumulate(nums.begin(),nums.end(),
(long long)5); to return long long instead of int.

iota(nums.begin(),nums.end(),100); - this starts nums with 100 and increases every


value by 1. If vector<int> nums(10); , then it becomes {100,101,102,.....,109,110}

To get a random value from a vector, use rand() as - nums[rand()%nums.size()]

To find the data type of a variable or function, use cout<<typeid(variable_name or


function).name();

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);

We can also do:


sort(intervals.begin(), intervals.end(), [&](int inter1, int inter2) {return
inter1 < inter2;});

Template classes can also be used

lower_bound: (can be used even with sets)


Lower bound of a number is the first number that is greater than or equal to
that particular number.
[10,20,30,40,50] - Lower bound of 30 is 30( returns index 2). Lower bound of
35 is 40( returns index 3).
Returns an iterator.
To get the index, do : lower_bound(nums.begin(),nums.end(),number)-
nums.begin();

upper_bound: (can be used even with sets)


[10,20,30,40,50] - Upper bound of 30 is 40( returns index 3). Upper bound of
60 is 5.
Returns an iterator.
To get the index, do : upper_bound(nums.begin(),nums.end(),number)-
nums.begin();

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

PRIORITY QUEUE (by default its max heap):

Declared as priority_queue<int> - max heap


Declared as priority_queue<int,vector<int>,greater<int>> - min heap
To declare a priority queue with elements as vector,
priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;
To make a priority queue from a vector, priority_queue<int>
pq(nums.begin(),nums.end());

empty()
top()
push()
pop()
emplace()

DEQUE (it is like both stack and queue):


It has O(1) insertion and deletion at both ends. O(1) access.

Declared as deque<int> dq;


push_front()
push_back()
pop_front()
pop_back()
front()
back()
empty()

MAP: (the key values are sorted in ascending order by default) - also has
unordered_map

m.insert(pair<int,int>(1,40)) - inserts the pair with corresponding values


begin()
end()
empty()
clear()
erase(g) - removes the key 'g'
Traverse it as - for (auto i: m) [here key is i.first and value is i.second]

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)

LIST: (linked list)

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.

To return just the first k elements of a DS like vector<vector<int>>, just do -


return vector<vector<int>>(points.begin(),points.begin()+k);

To sort and obtain the first k elements after sorting, do -


partial_sort(points.begin(), points.begin() + K, points.end(),cmp); P.S. cmp is
optional

You might also like