Download C++ Data Structure Cheat Sheet and more Cheat Sheet Data Structures and Algorithms in PDF only on Docsity! C++ Data Structures Cheat Sheet by Hackin7 via cheatography.com/71996/cs/18253/ Pointers Storing, data type of pointers and variables must be the same &var Returns address of memory location of variable data_type *pointer; Initia lize. Put * in front of name *pointer Returns value at the memory location stored by pointer Array variables are actually pointers to the first element in the array. The amount that your pointer moves from an arithmetic operation *array Returns first element in array *(array+2) Returns third element in array ◎ Variables that you declare are stored in a memory location in your computer ◎ The address of these memory locations can be stored in pointers ◎ Addresses are in hexade cimal Iterators //Append ::iterator to your data type declaration to create an iterator of that data type vector <in t>: :it erator it; // declares an iterator of vector <in t> // loops from the start to end of vi for(ve cto r<i nt> ::i terator it = vi.beg in(); it != vi.end(); it++) cout << *it << " "; // outputs 1 2 3 4 deque< int> d; deque< int >:: ite rator it; it = d.begin(); //Points to first element it++; // Points to next element it = d.end(); // Points to Last element it--; // Points to previous element cout << *it; // outputs the element it is pointing Iterators are essent ially pointers to an STL data structure Maps map<string, int> M; M[“hello”] = 2; M[“asd”] = 986; M.coun t(“ asd”); // returns 1 M.coun t(“ doe snt _ex ist”); // returns 0 M.size(); // Check for the existence of some key in the map - O(log N) it = M.find (“a sd”); //returns the iterator to “asd” it = M.uppe r_b oun d("a aa"); it = M.lowe r_b oun d("a aa"); if (it == M.end()) cout << "Does not exist \n"; //Iter ation for (auto it = mp.beg in(); it != mp.end(); ++it) { cout << it.first << “, “ << it.second << “\n”; } A data structure that takes in any data[key] Gives you the associated value stored through O(log N) magic Best used when you need to lookup certain values in O(log N) time that are associated with other values Queue queue<int> q; q.push(5); // Inserts/ Enqueue element at the back of the queue q.front(); // Returns element atb the front of the queue q.pop // Removes (Dequeues) element from the front of the queue q.empty(); // Returns boolean value of whether 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 cheatography.com/hackin7/ Published 12th December, 2018. Last updated 27th December, 2019. Page 1 of 5. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords! http://crosswordcheats.com C++ Data Structures Cheat Sheet by Hackin7 via cheatography.com/71996/cs/18253/ Priority Queue priority_queue<data_type> pq; // Largest at top priori ty_ que ue< dat a_type, vector <da ta_ typ e>, greate r<d ata _ty pe> > pq; // Smallest at top pq.pus h(5); // pushes element into it. Duplicates are allowed pq.top() // Returns largest or smallest element pq.pop() // Removes largest or smallest element pq.size(); // Returns size pq.emp ty(); Check if queue is empty Like a queue except that only the element with the greatest priority (eg. larges t/s mal lest) can be accessed Fenwick tree //Below here can mix & match long long ft[100 001]; // note: this fenwick tree is 1-indexed. ////PU ‐ PQ/ /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// // / ///// void fenwic k_u pda te(int pos, long long value) { while (pos <= N) { //c out <<"F enwick Updating: " <<p os< <","< ‐ <va lue <<endl; ft[pos] += value; pos += pos&-pos; } } long long fenwic k_q uer y(int pos) { long long sum = 0; while (pos) { // while p > 0 sum += ft[pos]; pos -= pos&-pos; } return sum; } ////RU ‐ PQ/ /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// // / ///// void fenwic k_r ang e_u pda te(int pos_a, int pos_b, int val){ //TLE way Fenwick tree (cont) //for (int i=pos_ a;i <=p os_ b;i ++) {fe nwi ck_ upd ‐ ate(i, val);} fen wic k_u pda te( pos_a, val); fen wic k_u pda te( pos _b+1, -val); } ////PU RQ/ /// /// /////// ////// /// /// /// /// /// /// /// /// /// /////// long long fenwic k_r ang e_q uer y(int pos_a, int pos_b) { return fenwic k_q uer y(p os_b) - fenwic k_q uer y(p ‐ os_ a-1); } ////RU ‐ RQ/ /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// // / ///// long long B1[100 001 ];long long B2[100 001]; void base_u pda te(long long *ft, int pos, long long value){ //Add largest power of 2 dividing x / Last set bit in number x for (; pos <= N; pos += pos&( -pos)) ft[pos] += value; } void rurq_r ang e_u pda te(int a, int b,long long v){ bas e_u pda te(B1, a, v); bas e_u pda te(B1, b + 1, -v); bas e_u pda te(B2, a, v * (a-1)); bas e_u pda te(B2, b + 1, -v * b); } void rurq_p oin t_u pda te(int a, long long v){ rur q_r ang e_u pda te( a,a,v); } long long base_q uer y(long long *ft,int b){ long long sum = 0; for(; b > 0; b -= b& (-b)) sum += ft[b]; return sum; } // Return sum A[1...b] long long rurq_q uer y(int b){ By Hackin7 cheatography.com/hackin7/ Published 12th December, 2018. Last updated 27th December, 2019. Page 2 of 5. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords! http://crosswordcheats.com