Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / Array2D.cpp
Last active March 26, 2022 17:37
A 2-dimensional array class
#include <stdexcept>
// uses the Array<T> class
template <class T>
class Array2D
{
protected:
unsigned int numberOfRows, numberOfColumns;
Array<T> array;
public:
@dgodfrey206
dgodfrey206 / consecutive_find.cpp
Created November 18, 2014 20:03
consecutive_find() is a function that returns the beginning of a range that contains strictly N consecutive elements. It was part of a SO question that I answered.
#include <iostream>
#include <vector>
#include <algorithm>
template <class Iter>
Iter consecutive_find(Iter first, Iter last, std::size_t n)
{
Iter marker(first), lead(first);
std::size_t count(1);
@dgodfrey206
dgodfrey206 / alphafmt.cpp
Created November 14, 2014 00:45
Facet that ignores all alphabetical characters
#include <iostream>
#include <sstream>
class whitespace_fmt : public std::ctype<char>
{
mask table[table_size];
public:
whitespace_fmt(size_t refs = 0)
: std::ctype<char>(table, false, refs)
{
@dgodfrey206
dgodfrey206 / Employee.cpp
Created November 6, 2014 18:56
Employee class with I/O
#include <iostream>
#include <sstream>
#include <string>
#include <boost/io/ios_state.hpp>
template <class cT>
class whitespace_fmt;
template <>
class whitespace_fmt<char> : public std::ctype<char>
@dgodfrey206
dgodfrey206 / filename.cpp
Created November 6, 2014 17:05
A facet that holds a stream's filename
#include <iostream>
#include <fstream>
struct filename_facet : std::locale::facet
{
private:
std::string m_name;
public:
filename_facet(std::string const& name, std::size_t refs = 0)
: std::locale::facet(refs)
@dgodfrey206
dgodfrey206 / two-stack.cpp
Last active April 15, 2022 15:40
Using Dijkstra's two-stack algorithm to parse simple arithmetic expressions
#include <iostream>
#include <string>
#include <stack>
void calculate(std::stack<int>& numbers, std::stack<char>& operators);
int main()
{
std::stack<int> numbers;
std::stack<char> operators;
@dgodfrey206
dgodfrey206 / compose.cpp
Last active August 29, 2015 14:07
Allows a functional style syntax for composing functions
#include <functional>
template<class T>
struct wrap_impl
{
wrap_impl(T&& x) : data(std::forward<T>(x)) { }
T get() const & { return data; }
T&& get() && { return std::move(data); }
private:
@dgodfrey206
dgodfrey206 / tuple_fold.cpp
Last active August 29, 2015 14:07
A right heterogeneous fold on variadic tuples.
#include <tuple>
#include <type_traits>
#include <utility>
#include <integer_sequence>
template <class F, class Tuple, size_t... I>
auto tuple_map_impl(F f, Tuple&& t, index_sequence<I...>)
{
return std::make_tuple(f(std::get<I>(std::forward<Tuple>(t)))...);
}
@dgodfrey206
dgodfrey206 / has_type_v3.cpp
Last active August 29, 2015 14:07
The third improved version of has_type. This version exhibits behavior equivalent to short circuiting with no recursion whatsoever.
#include <type_traits>
#include <tuple>
namespace detail
{
template <typename value, typename tuple>
struct has_type;
template <typename value, typename... xs>
struct has_type<value, std::tuple<xs...>>
@dgodfrey206
dgodfrey206 / has_type_v2.cpp
Last active August 29, 2015 14:07
The second improved version of has_type. It's shorter, does away with recursion and uses the bool_sequence idiom.
#include <type_traits>
#include <tuple>
template <bool...>
struct bool_sequence { };
template <bool... Bs>
using bool_and = std::is_same<bool_sequence<Bs...>,
bool_sequence<(Bs || true)...>>;