Skip to content

Instantly share code, notes, and snippets.

@Hysperr
Hysperr / main.cpp
Last active September 26, 2018 16:05
C++ std::unique_ptr scoping and move semantics
// C++ Gotcha #54,638,405
#include <iostream>
#include <memory>
#include <vector>
// EXAMPLE 1
int *p;
// EXAMPLE 2
@Hysperr
Hysperr / advanced_cplusplus.cpp
Last active July 24, 2017 05:05
Advanced C++ - Examining lambda functions, function pointers, initializer lists, move semantics, constructor inheritance, suffix return, and type_traits.
#include <vector>
#include <iostream>
#include <string>
class AddressBook {
public:
AddressBook() = default;
@Hysperr
Hysperr / lambda-random-functors.cpp
Last active July 23, 2017 10:29
Understanding lambda functions and <random> header for a mersenne twister engine producing 32-bit pseudo-random integers viewed through a uniform int distribution.
#include <iostream>
#include <random>
#include <algorithm>
void lessFiftyA(std::vector<int> &v) {
std::transform(v.begin(), v.end(), v.begin(), [&](int i) -> bool {
return i < 50;
});
}
@Hysperr
Hysperr / greedy_stocks.cpp
Last active July 22, 2017 20:31
Greedy algorithm to find best buy and sell for stocks. Taking locally optimum choice. O(n) runtime complexity.
#include <vector>
#include <iostream>
using namespace std;
void getBestStock(const std::vector<int> &v) {
int minprice = v[0];
int maxProfit = v[1] - v[0];
int potential;
for (int i = 1; i < int(v.size()); ++i) {
@Hysperr
Hysperr / ActivitySelectorProblem.java
Last active July 22, 2017 07:35
Greedy Algorithm: Activity Selection
/**
* This program demonstrates the Activity Selection Problem. It is implemented using a greedy
* algorithm which works by making the locally optimal choice at each stage with the hope of
* finding a global optimum. The average run time for this implementation is Theta(n).
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
@Hysperr
Hysperr / my_atoi.cpp
Last active July 22, 2017 06:55
Manually convert ASCII to integer using two different methods. The purpose is to reinforce discrete math concepts and increase ASCII table familiarity.
#include <cmath>
#include <string>
#include <iostream>
// http://www.asciitable.com/
/**
* The idea is simple. Loop through string, and if the char is valid
* for conversion, continue, else return what you have (op failure).
* Subtracting the char (that represents a digit) from 0 gives it's
@Hysperr
Hysperr / function_template_parameters.cpp
Last active July 23, 2017 10:32
Demonstrates function template parameters in C++. Key is understanding templates during the compiling & linking process.
#include <iostream>
void convert_to_upper(char &c) {
c = (char) std::toupper(c);
}
template<typename Process, typename T, typename SizeType>
void apply_chararray(Process f, T data[], SizeType n) {
std::cout << __func__ << std::endl;
for (int i = 0; i < n; i++) {
@Hysperr
Hysperr / recursive_arithmetic.cpp
Last active July 23, 2017 10:39
Recursive summation and factorial functions using conditional ternary operator ( condition ? expr1 : expr2 ) and binary conversion using std::bitset
#include <iostream>
#include <bitset>
#include <limits>
/** summation */
constexpr int s1(int n) {
return n > 0 ? n + s1(n - 1) : n;
}
/** factorial */
@Hysperr
Hysperr / data_frequency.cpp
Last active July 22, 2017 07:07
Advanced Data Structures: This gist uses std::unordered_map<K,V> to find the occurring frequency of homogeneous data sets. A comparator function or comparator object must be written. Move semantics are used. We'll use colors strings for sample data and demonstrate a few best practices in C++11.
#include <array>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
typedef std::string m_key;
typedef int m_value;
// Comparator function. map objects take in std::pairs. Passed as rvalues to const references.
/*
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() <<std::endl;
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() <<std::endl;
*/
/* Testing stack references. Conclusion follows. Place into gist eventually.