Hackin7 - C Data Structures
Hackin7 - C Data Structures
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 Initialize. Put * in front of name
*pointer; M.count(“asd”); // returns 1
M.count(“doesnt_exist”); // 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(“asd”); //returns the iterator to
*array Returns first element in array
“asd”
*(array+2) Returns third element in array it = M.upper_bound("aaa");
◎ Variables that you declare are stored in a memory location in your it = M.lower_bound("aaa");
computer if (it == M.end())
◎ The address of these memory locations can be stored in pointers cout << "Does not exist\n";
◎ Addresses are in hexadecimal //Iteration
for (auto it = mp.begin(); 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<int>::iterator it; // declares an iterator
of vector<int> 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(vector<int>::iterator it = vi.begin(); 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>::iterator 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 essentially 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
// OR Like arrays but re-sizable. You can add and remove any number of
pair<data_type_1, data_type_2> variable = make_p‐ elements from any position.
air(value1,value2);
// Store values Sets and Multisets
variable.first = value; set<int> s; set<int>::iterator it;
variable second = value; multiset<int> s; multiset<int>::iterator it;
// Retrieve values s.insert(10);
cout <<variable first << " " << variable.second << it = s.find(8)
endl; it = s.upper_bound(7);
//Nesting pairs it = s.lower_bound(7);
pair<int, pair<int, int> > a; s.erase(10); //Remove element from set
a.first = 5; s.erase(it) //Can also use iterators
a.second.first = 6; s.empty();
a.second.second = 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
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} right->lazyadd += lazyadd;
d[0]; // returns a value of 2 left->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_back(5); // add an element to the back - }
O(1)
d.push_front(10); // add an element to the front - void addRange(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_front(); // 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 right->addRange(lower_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){
left->addRange(lower_bound,
Deque = Double Ended Queue! upper_bound, val_to_add);
}else{
Segment Tree left->addRange(lower_bound, mid,
val_to_add);
right->addRange(mid+1, upper_‐
struct node {
bound, val_to_add);
int start, end, mid, val, lazyadd;
}
node left, right;
val = min(left->value(), right->va‐
lue());
node(int _s, int _e) {
}
//Range of values stored
}
start = _s; end = _e; mid = (start+end)/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(start,mid);
if (start==end) { val=new_val; return; }
right = new node(mid+1,end);
if (pos>mid) right->update(pos, new_val);
}
if (pos<=mid) left->update(pos, new_val);
}
val = min(left->val, right->val);
}
int value(){
if (start==end){
// Range Minimum Query // O(log N)
val += lazyadd;lazyadd = 0;return val;