@@ -354,6 +354,8 @@ exp(x); log(x); log10(x); // e to the x, log base e, log base 10
354354pow(x, y); sqrt(x); // x to the y, square root
355355ceil(x); floor(x); // Round up or down (as a double)
356356fabs(x); fmod(x, y); // Absolute value, x mod y
357+ double res = double(expenditure[ mid1] + expenditure[ mid2] )/2; //keep floating point
358+ 5/2 = 2 // int division returns floor
357359```
358360
359361## `assert.h`, `cassert` (Debugging Aid)
@@ -411,6 +413,11 @@ getline(cin, s); // Read line ending in '\n'
411413for (char const &c: s) {
412414 std::cout << c << ' ';
413415}
416+
417+ vector<int > freq(26, 0);
418+ for(auto c: a){
419+ freq[ c-'a'] +=1;
420+ }
414421```
415422
416423## `vector` (Variable sized array/stack with built in memory allocation)
@@ -433,6 +440,11 @@ for (vector<int>::iterator p=a.begin(); p!=a.end(); ++p)
433440vector<int> b(a.begin(), a.end()); // b is copy of a
434441vector<T> c(n, x); // c[0]..c[n-1] init to x
435442T d[10]; vector<T> e(d, d+10); // e is initialized from d
443+ assert(!v.empty());
444+ v.erase(v.begin());
445+ bool myfunction (int i,int j) { return (i<j); }
446+ sort(prices.begin(), prices.end(), myfunction);
447+
436448```
437449
438450## ` deque ` (Array stack queue)
@@ -444,7 +456,7 @@ T d[10]; vector<T> e(d, d+10); // e is initialized from d
444456a.push_front(x); // Puts x at a[0], shifts elements toward back
445457a.pop_front(); // Removes a[0], shifts toward front
446458```
447-
459+
448460## ` utility ` (pair)
449461
450462``` cpp
@@ -474,6 +486,13 @@ a["hello"] = 3; // Add or replace element a["hello"]
474486for (auto & p:a)
475487 cout << p.first << p.second; // Prints hello, 3
476488a.size(); // 1
489+
490+ // find
491+ std::unordered_map<std::string,double >::const_iterator got = mymap.find (input);
492+ if ( got == mymap.end() )
493+ std::cout << " not found" ;
494+ else
495+ std::cout << got->first << " is " << got->second;
477496```
478497
479498## ` set ` (store unique elements - usually implemented as binary search trees - avg. time complexity: O(log n))
0 commit comments