Skip to content

Instantly share code, notes, and snippets.

// How do you find the duplicate number on a given integer array?
#include <iostream>
#include <algorithm>
// core search algorithm
int find_max (int* _counter)
{
int i;
for ( i = 0; i < 100; ++i )
// How do you find the missing number in a given integer array of 1 to 100?
#include <iostream>
#include <algorithm>
int arr[100];
int main()
{
for( int i = 0; i < 100; ++i )
#include "../CppLibs/Eigen/eigen-3.3.7/Eigen/Dense"
#include <iostream>
#define print(s) std::cout << s << std::endl //I know, I know. But it's one line and won't break anything.
// I thought about using lambdas for print to be nice but it's no bueno, and I'm not using namespace std and I'm not using << explicitly. It's so ugly. now I'm an outlaw using preprocessor commands on a whim. if you don't like it you're going to need to pry my print() function from my cold, dead hands.//auto print = [](std::string s){std::cout << s << std::endl;}; //I know 4 ways to do this now - typedef, define, using, and lambdas. no preprocessor stuff outside of ifndef. I wanted to template the lambda because I want to be able to print anything - but of course the lambda works using function pointers, and templates make multiple functions based on the use case and thus there are multiple function pointers - so of course it doesn't work... But templated lambdas will be introduced in C++20! Excited for this - should be out this mo
#include <iostream>
#include <vector>
int main()
{
auto square = [](int num){return num*num;};
auto print = [](int s){std::cout << s << std::endl;};
std::vector<int> values = { 0, 1, 2, 3, 4, 5 };
#include <iostream>
#include <vector>
void Print(int s)
{
std::cout << s << std::endl;
}
void ForEach(const std::vector<int>& values, void(*func)(int))
{
#include <iostream>
using namespace std;
template <typename T>
T square (T x) {
return x*x;
}
int main() {
cout << (5<4 ? "T" : "F") << endl;
#include <iostream>
#include <string>
using String = std::string;
int main () {
try {
String my_str = "Yikes";
throw my_str;
}
catch (String e) {
#include <iostream>
#include <string>
struct vector3 { float x, y, z; }; // struct or class, doesn't matter
int main() {
int value = 5; // allocated on the stack
int array[5];
vector3 vector;
array[0] = 1;
#include <iostream>
#define print(s) std::cout << s << std::endl;
int increment0(int value) {
value++;
return value;
}
void increment1(int& value) {
value++;
#include <iostream>
#define LOG(x) std::cout << x << std::endl;
int main()
{
char* buffer = new char[8];
int a = 5;
void* ptr = &a;
LOG(*(int*)ptr);