This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C++ Gotcha #54,638,405 | |
#include <iostream> | |
#include <memory> | |
#include <vector> | |
// EXAMPLE 1 | |
int *p; | |
// EXAMPLE 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
#include <string> | |
class AddressBook { | |
public: | |
AddressBook() = default; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bitset> | |
#include <limits> | |
/** summation */ | |
constexpr int s1(int n) { | |
return n > 0 ? n + s1(n - 1) : n; | |
} | |
/** factorial */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |