0% found this document useful (0 votes)
21 views

Hackin7 - C Data Structures

Uploaded by

vishalpalv43004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Hackin7 - C Data Structures

Uploaded by

vishalpalv43004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ Data Structures Cheat Sheet

by Hackin7 via cheatography.com/71996/cs/18253/

Pointers Maps

Storing, data type of pointers and variables must be the same map<string, int> M;
&var Returns address of memory location of variable M[“hello”] = 2;
M[“asd”] = 986;
data_type Initia​lize. Put * in front of name
*pointer; M.coun​t(“​asd”); // returns 1
M.coun​t(“​doe​snt​_ex​ist”); // returns 0
*pointer Returns value at the memory location stored by
M.size();
pointer
// Check for the existence of some key in the map -
Array variables are actually pointers to the first element in the array.
O(log N)
The amount that your pointer moves from an arithmetic operation
it = M.find​(“a​sd”); //returns the iterator to
*array Returns first element in array
“asd”
*(array+2) Returns third element in array it = M.uppe​r_b​oun​d("a​aa");
◎ Variables that you declare are stored in a memory location in your it = M.lowe​r_b​oun​d("a​aa");
computer if (it == M.end())
◎ The address of these memory locations can be stored in pointers ​ ​ ​ cout << "Does not exist​\n";
◎ Addresses are in hexade​cimal //Iter​ation
for (auto it = mp.beg​in(); it != mp.end(); ++it) {
Iterators ​ ​ ​ cout << it.first << “, “ << it.second << “\n”;
//Append ::iterator to your data type declaration }
to create an iterator of A data structure that takes in any data[key]
that data type Gives you the associated value stored through O(log N) magic
vector​<in​t>:​:it​erator it; // declares an iterator
of vector​<in​t> Best used when you need to lookup certain values in O(log N) time
// loops from the start to end of vi that are associated with other values
for(ve​cto​r<i​nt>​::i​terator it = vi.beg​in(); it !=
vi.end(); it++) Queue
​ ​ ​ cout << *it << " "; // outputs 1 2 3 4 queue<int> q;
deque<​int> d; q.push(5); // Inserts/ Enqueue element at the back
deque<​int​>::​ite​rator it; of the queue
it = d.begin(); //Points to first element q.front(); // Returns element atb the front of the
it++; // Points to next element queue
it = d.end(); // Points to Last element q.pop // Removes (Dequeues) element from the front
it--; // Points to previous element of the queue
cout << *it; // outputs the element it is pointing q.empty(); // Returns boolean value of whether
Iterators are essent​ially pointers to an STL data structure queue is empty

First In First Out data structure where elements can only be added to
the back and accessed at the front

By Hackin7 Published 12th December, 2018. Sponsored by CrosswordCheats.com


cheatography.com/hackin7/ Last updated 27th December, 2019. Learn to solve cryptic crosswords!
Page 1 of 5. http://crosswordcheats.com
C++ Data Structures Cheat Sheet
by Hackin7 via cheatography.com/71996/cs/18253/

Priority Queue Fenwick tree (cont)

priority_queue<data_type> pq; // Largest at top ​ ​ ​ ​//for (int i=pos_​a;i​<=p​os_​b;i​++)​{fe​nwi​ck_​upd​‐


priori​ty_​que​ue<​dat​a_type, vector​<da​ta_​typ​e>, ate(i, val);}
greate​r<d​ata​_ty​pe> > ​ ​ ​ ​fen​wic​k_u​pda​te(​pos_a, val);
pq; // Smallest at top ​ ​ ​ ​fen​wic​k_u​pda​te(​pos​_b+1, -val);
pq.pus​h(5); // pushes element into it. Duplicates }
are allowed ////PU​RQ/​///​///​///////
pq.top() // Returns largest or smallest element //////​///​///​///​///​///​///​///​///​///​///////
pq.pop() // Removes largest or smallest element long long fenwic​k_r​ang​e_q​uer​y(int pos_a, int
pq.size(); // Returns size pos_b) {
pq.emp​ty(); Check if queue is empty ​ ​ ​ ​return fenwic​k_q​uer​y(p​os_b) - fenwic​k_q​uer​y(p​‐
os_​a-1);
Like a queue except that only the element with the greatest priority
(eg. larges​t/s​mal​lest) can be accessed }
////RU​‐
Fenwick tree RQ/​///​///​///​///​///​///​///​///​///​///​///​///​///​///​///​//
/​/////
//Below here can mix & match
long long B1[100​001​];long long B2[100​001];
long long ft[100​001]; // note: this fenwick tree
void base_u​pda​te(long long *ft, int pos, long long
is 1-indexed.
value){
////PU​‐
​ ​//Add largest power of 2 dividing x / Last set
PQ/​///​///​///​///​///​///​///​///​///​///​///​///​///​///​///​//
bit in number x
/​/////
​ for (; pos <= N; pos += pos&(​-pos))
void fenwic​k_u​pda​te(int pos, long long value) {
​ ​ ​ ​ft[pos] += value;
​ ​ ​ ​while (pos <= N) {
}
​ ​ ​ ​ ​ ​ ​ ​//c​out​<<"F​enwick Updating: "​<<p​os<​<","<​‐
void rurq_r​ang​e_u​pda​te(int a, int b,long long v){
<va​lue​<<endl;
​ ​bas​e_u​pda​te(B1, a, v);
​ ​ ​ ​ ​ ​ ​ ​ft[pos] += value;
​ ​bas​e_u​pda​te(B1, b + 1, -v);
​ ​ ​ ​ ​ ​ ​
​ ​bas​e_u​pda​te(B2, a, v * (a-1));
​ ​ ​ ​ ​ ​ ​ pos += pos&-pos;
​ ​bas​e_u​pda​te(B2, b + 1, -v * b);
​ ​ ​ }
}
}
void rurq_p​oin​t_u​pda​te(int a, long long v){
long long fenwic​k_q​uer​y(int pos) {
​ ​ ​ ​rur​q_r​ang​e_u​pda​te(​a,a,v);
​ ​ ​ long long sum = 0;
}
​ ​ ​ ​while (pos) { // while p > 0
long long base_q​uer​y(long long *ft,int b){
​ ​ ​ ​ ​ ​ ​ sum += ft[pos];
​ long long sum = 0;
​ ​ ​ ​ ​ ​ ​ pos -= pos&-pos;
​ ​for(; b > 0; b -= b&​(-b))
​ ​ ​ }
​ ​ ​ sum += ft[b];
​ ​ ​ ​return sum;
​ ​return sum;
}
}
////RU​‐
// Return sum A[1...b]
PQ/​///​///​///​///​///​///​///​///​///​///​///​///​///​///​///​//
long long rurq_q​uer​y(int b){
/​/////
void fenwic​k_r​ang​e_u​pda​te(int pos_a, int pos_b, int
val){
​ ​ ​ ​//TLE way

By Hackin7 Published 12th December, 2018. Sponsored by CrosswordCheats.com


cheatography.com/hackin7/ Last updated 27th December, 2019. Learn to solve cryptic crosswords!
Page 2 of 5. http://crosswordcheats.com
C++ Data Structures Cheat Sheet
by Hackin7 via cheatography.com/71996/cs/18253/

Fenwick tree (cont) Vector

​ ​return base_q​uer​y(B1, b) * b - base_q​uer​y(B2, b); // Initialize


} vector​<da​ta_​typ​e> v;
//Return sum A[a...b] v.push​_ba​ck(​value); // Add element to back
long long rurq_r​ang​e_q​uer​y(int a,int b){ v.pop_​back() // Remove last element
​ ​return rurq_q​uery(b) - rurq_q​uer​y(a-1); v.clear(); // Remove all elements
} v[index] // Return element of index
v.back(); // Return last element
Pair v.size(); // Return Size of vector

// Initialise v.empty() // Return boolean value of whether

pair<d​ata​_ty​pe_1, data_t​ype​_2> variable; vector is empty

// OR Like arrays but re-siz​able. You can add and remove any number of
pair<d​ata​_ty​pe_1, data_t​ype​_2> variable = make_p​‐ elements from any position.
air​(va​lue​1,v​alue2);
// Store values Sets and Multisets
variab​le.f​irst = value; set<int> s; set<int>::iterator it;
variable second = value; multis​et<​int> s; multis​et<​int​>::​ite​rator it;
// Retrieve values s.inse​rt(10);
cout <<v​ariable first << " " << variab​le.s​econd << it = s.find(8)
endl; it = s.uppe​r_b​oun​d(7);
//Nesting pairs it = s.lowe​r_b​oun​d(7);
pair<int, pair<int, int> > a; s.eras​e(10); //Remove element from set
a.first = 5; s.eras​e(it) //Can also use iterators
a.seco​nd.f​irst = 6; s.empty();
a.seco​nd.s​econd = 7; s.clear();
Stores a pair of values // to loop through a set
for(it = s.begin(); it != s.end(); it++)
Stack ​ ​ ​ cout << *it << " "; // outputs 2 7 10

stack<int> s; In a set: All elements are sorted and no duplicates


s.push(5); // push an element onto the stack - Multisets can store duplicates though
O(1)
s.pop(); // pop an element from the stack - O(1)
s.top(); // access the element at the top of the
stack - O(1)
s.empty(); // whether stack is empty - O(1)

First-​In-​Las​t-Out data structure


Only Element at the top can be accessed / removed

By Hackin7 Published 12th December, 2018. Sponsored by CrosswordCheats.com


cheatography.com/hackin7/ Last updated 27th December, 2019. Learn to solve cryptic crosswords!
Page 3 of 5. http://crosswordcheats.com
C++ Data Structures Cheat Sheet
by Hackin7 via cheatography.com/71996/cs/18253/

Deque Segment Tree (cont)

deque<int> d; ​ ​ ​ ​ ​ ​ ​ ​}else{
// access an element / modify an element (0-indexed ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ val += lazyadd;
as well) - O(1) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ // Propagate Lazyadd
d[0] = 2; // change deque from {5, 10} to {2, 10} ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​rig​ht-​>la​zyadd += lazyadd;
d[0]; // returns a value of 2 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​lef​t->​lazyadd += lazyadd;
d.back(); // get back (last) element - O(1) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​lazyadd = 0;
d.front(); // get front (first) element - O(1) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return val;
d.clear() // Remove all elements ​ ​ ​ ​ ​ ​ ​ }
d.push​_ba​ck(5); // add an element to the back - ​ ​ ​ }
O(1) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
d.push​_fr​ont​(10); // add an element to the front - ​ ​ ​ void addRan​ge(int lower_​bound, int upper_​‐
O(1) bound, int val_to​_add){
d.pop_​back(); // remove the back (last) element - ​ ​ ​ ​ ​ ​ ​ if (start == lower_​bound && end == upper_​‐
O(1) bound){
d.pop_​fro​nt(); // remove the front (first) element ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​lazyadd += val_to​_add;
- O(1) ​ ​ ​ ​ ​ ​ ​ ​}else{
d.size(); //Return size ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ if (lower​_bound > mid){
d.empty // Whether queue is empty ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​rig​ht-​>ad​dRa​nge​(lo​wer​_bound,
upper_​bound, val_to​_add);
A stack and queue combined.
...or a vector that and be pushed and popped from the front as well. ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​}else if (upper​_bound <= mid){
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​lef​t->​add​Ran​ge(​low​er_​bound,
Deque = Double Ended Queue! upper_​bound, val_to​_add);
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​}else{
Segment Tree ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​lef​t->​add​Ran​ge(​low​er_​bound, mid,
val_to​_add);
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​rig​ht-​>ad​dRa​nge​(mid+1, upper_​‐
struct node {
bound, val_to​_add);
​ ​ ​ int start, end, mid, val, lazyadd;
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ node left, right;
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ val = min(le​ft-​>va​lue(), right-​>va​‐
​ ​ ​
lue());
​ ​ ​ ​nod​e(int _s, int _e) {
​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ ​//Range of values stored
​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ ​start = _s; end = _e; mid = (start​+en​d)/2;
​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​//Min value stored
​ ​ ​ // Update position to new_value // O(log N)
​ ​ ​ ​ ​ ​ ​ val = 0; lazyadd = 0;
​ ​ ​ void update(int pos, int new_val) { //position
​ ​ ​ ​ ​ ​ ​ if (start​!=end) {
x to new value
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ left = new node(s​tar​t,mid);
​ ​ ​ ​ ​ ​ ​ if (start​==end) { val=ne​w_val; return; }
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​right = new node(m​id+​1,end);
​ ​ ​ ​ ​ ​ ​ if (pos>mid) right-​>up​dat​e(pos, new_val);
​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ if (pos<=mid) left->​upd​ate​(pos, new_val);
​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ val = min(le​ft-​>val, right-​>val);
​ ​ ​
​ ​ ​ }
​ ​ ​ int value(){
​ ​ ​
​ ​ ​ ​ ​ ​ ​ if (start​==end){
​ ​ ​ // Range Minimum Query // O(log N)
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ val += lazyad​d;l​azyadd = 0;return val;

By Hackin7 Published 12th December, 2018. Sponsored by CrosswordCheats.com


cheatography.com/hackin7/ Last updated 27th December, 2019. Learn to solve cryptic crosswords!
Page 4 of 5. http://crosswordcheats.com
C++ Data Structures Cheat Sheet
by Hackin7 via cheatography.com/71996/cs/18253/

Segment Tree (cont)

​ ​ ​ int rangeM​ini​mum​Que​ry(int lower_​bound, int


upper_​bound) {
​ ​ ​ ​ ​ ​ ​ ​//c​out​<<"N​ode​:"<<​sta​rt<​<" "​<<e​nd<​<" "​<<m​id<​<"
"​<<v​al<​<endl;
​ ​ ​ ​ ​ ​ ​ //If Query Range Corres​pon​ds/​///​///​///​//////
​ ​ ​ ​ ​ ​ ​ if (start​==l​owe​r_bound && end==u​ppe​r_b​ound){
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return value();
​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ ​//Query Right Tree if range only lies there
​ ​ ​ ​ ​ ​ ​ else if (lower​_bound > mid){
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return right-​>ra​nge​Min​imu​mQu​ery​(lo​wer​‐
_bound, upper_​bound);
​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ ​//Query Left Tree if range only lies there
​ ​ ​ ​ ​ ​ ​ else if (upper​_bound <= mid){
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return left->​ran​geM​ini​mum​Que​ry(​low​er_​‐
bound, upper_​bound);
​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ ​//Query both ranges as range spans both trees
​ ​ ​ ​ ​ ​ ​ ​else{
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return min(le​ft-​>ra​nge​Min​imu​mQu​ery​(lo​wer​‐
_bound, mid),r​igh​t->​ran​geM​ini​mum​Que​ry(​mid+1, upper_​‐
bou​nd));
​ ​ ​ ​ ​ ​ ​ }
​ ​ ​ ​ ​ ​ ​ ​//E​‐
nd/​///​///​///​///​///​///​///​///​///​///​///​///​/////
​ ​ ​ }
​ ​ ​
} *root;
void init(int N){
​ ​ ​ root = new node(0, N-1); // creates seg tree of
size n
}
void update(int P, int V){
​ ​ ​ ​roo​t->​upd​ate​(P,V);
}
int query(int A, int B){
​ ​ ​ int val = root->​ran​geM​ini​mum​Que​ry(​A,B);
​ ​ ​ ​return val;
}

By Hackin7 Published 12th December, 2018. Sponsored by CrosswordCheats.com


cheatography.com/hackin7/ Last updated 27th December, 2019. Learn to solve cryptic crosswords!
Page 5 of 5. http://crosswordcheats.com

You might also like