C++ Notes
C++ Notes
Flushing Buffer:
Time:
time_t t = time(NULL)
Sets and multisets: set<string> strset, strset = {…} cannot have duplicate, multiset<string> strset, strset = {…} can have duplicate
.size(), .insert(“Value”) this returns pair<set<string>::iterator, bool>, .find(“Helmp”) returns iterator to found if found, else returns an iterator to end, .erase(it)
.erase(“Value”) .begin() .end() tie(a, b, c) = strset .count(“Value”) in sets returns 1 or 0 .lower_bound(“Value”) .upper_bound(“Value”)
We cannot random access in a set which makes sense because they all remain sorted. They are like dictionaries
There is also unordered_set<> and unordered_multiset<> that uses hashes, while sets and multisets uses balanced binary tree
Maps: map<string, string> my_map, my_map = {{George, Father}, {Evelyn, Mother}} mutimap<string, string> my_dict = {{..},{..},…}
.find(“George”) returns an iterator to the pair, .first, .second, .begin(), .end(), .size(), .insert({ "Luke", "Neighbor" })
my_map[“George”] will not work for mutimaps, .count(“George”)
Maps are like sets of pairs (sorted w.r.t the first index). They are used for hashes: map<int, vector<string>>
There are also unordered_map;
Queue (FIFO): queue<int, list<int>> ql(li), queue<string> qd deque
.size() .front() .empty() .pop() .push("one") .back()
and Stack (LIFO):
.size() .top() .empty() .pop() .push("one")
Priority queue by default maxheap: priority_queue<int> gquiz priority_queue<int, vector<int>, greater<int>> gquiz, pq(arr, arr + size)minheaps
.size() .top() .empty() .pop() .push(1) .swap(gpquiz)
Strings:
.size() .length() .front() .back() .begin() .end() .rbegin() .resize(num) .insert(it, ‘X’) .erase(it) .push_back(‘S’)
.replace(pos from front, no of char, new substr) .substr(pos from front, no of char) .substr(pos from front) .find(‘s’) .rfind(‘s’)
.find_first_of(‘S’) .npos .append(str) .compare(str)
Algorithms: (inline funcs mean function pointers, function operators (functors) and lambda functions)
*max_element (first_iterator, last_iterator) *min_element (first_iterator, last_iterator) accumulate(first_iterator, last_iterator, initial value of sum)
count(first_iterator, last_iterator, x) find(first_iterator, last_iterator, x) binary_search(first_iterator, last_iterator, x) lower_bound(first_iterator, last_iterator, x)
upper_bound(first_iterator, last_iterator, x) distance(first_iterator, desired_position) search(haystack it1, haystack it2, needle it1, needle it2)
equal(start it, middle it, end it) count_if(start it, end it, inline funcs) .erase(unique(arr.begin(),arr.end()),arr.end()) sort(first_iterator, last_iterator)
reverse(first_iterator, last_iterator) next_permutation(first_iterator, last_iterator) prev_permutation(first_iterator, last_iterator) for_each(it1, it2, inline funcs)
transform(main it1, main it2, store it, inline funcs) random_shuffle(first it, last it) equal_range(first_iterator, last_iterator, x)
distance(first_iterator, last_iterator), to_string(number)
move(b.begin(), b.end(), std::back_inserter(a)) copy(b.begin(), b.end(), std::back_inserter(a))
void qsort (void* array, size_t array_size, size_t size_of_element, int (*comparator) (const void*,const void*));
Policy Based Data Structures: indexed_set s
.insert(“Value”) .find_by_order(pos) .order_of_key(“Value”)
Must: #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds;
This is like a set that supports random access
>FUNCTION POINTERS: Note: return_type and parameters must be same for fptr and func
return_type (*fptr) ( parameters ) = func | use: var = (*fptr)( parameters ) [ Example: void (*x) ( ) = func; ]
return_type (*fptr[ ]) ( parameters ) = {a , b, c, d, e, NULL} | use: var = fptr[i] ( params ) [ Example: void (*x[ ]) ( ) = {a , b, c, d, e, NULL}; ]
vector< return_type (*) () > fptr = {a , b, c, d, e} } | use: var = fptr[i] ( params ) ) [ Example: vector< int (*) ( ) > x = {a , b, c, d, e }; ]
python regex:
https://www.youtube.com/watch?v=K8L6KVGG-7o