Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / gist:8985329
Created February 13, 2014 22:33
Setting characters as whitespace
#include <iostream>
#include <sstream>
#include <vector>
namespace detail
{
enum options { add, remove };
class ctype : public std::ctype<char>
{
@dgodfrey206
dgodfrey206 / gist:9975127
Last active August 29, 2015 13:58
Program that allows an arbitrary separator between each output sequence.
#include <iostream>
namespace custom
{
struct sep_impl
{
sep_impl(std::string const& separator);
std::string separator;
};
@dgodfrey206
dgodfrey206 / gist:bc442ee343ceaf55e8a0
Created May 2, 2014 21:38
Turning a tuple to a string
#include <iostream>
#include <vector>
#include <string>
namespace detail
{
template <int... Is>
struct index { };
template <int N, int... Is>
@dgodfrey206
dgodfrey206 / gist:9372ca6d76401a1aee5c
Created May 11, 2014 20:25
A stream buffer that capitalizes every word of a sentence.
#include <iostream>
class stream_buffer : public std::streambuf
{
public:
stream_buffer(std::ostream& os)
: whitespace(true),
ctype(std::use_facet<std::ctype<char>>(getloc())),
m_sbuf(os.rdbuf())
{ }
@dgodfrey206
dgodfrey206 / gist:3bbe83852bedc6555b2a
Created May 13, 2014 22:20
Word extractor part 2
#include <iostream>
#include <sstream>
#include <typeinfo>
namespace detail
{
template<int... Is>
struct index { };
template<int N, int... Is>
@dgodfrey206
dgodfrey206 / gist:c0fd8e0b34a652c460a5
Last active August 29, 2015 14:01
A std::num_get facet tells the user what kind of number they entered (positive or negative), and does not accept input of floating point form.
class num_get : public std::num_get<char>
{
public:
// Override do_get which is a virtual function in the std::num_get<char>
// base class. It is called by the public member function get() in the
// implementation of std::basic_istream<charT>::operator>>(int&)
// You might want to put this into a helper function and call it in
// both the signed and unsigned overloads
@dgodfrey206
dgodfrey206 / gist:62b851d1e02d953951ad
Last active August 29, 2015 14:01
A num_put facet that appends a prepends and appends text to an output operation
#include <locale>
template<class char>
class num_put : public std::num_put<char>
{
using string_type = std::basic_string<char>;
string_type first_, second_;
public:
num_put(const string_type& first = string_type{},
@dgodfrey206
dgodfrey206 / gist:cbfc45712e547913fb16
Last active August 29, 2015 14:02
An input stream iterator that constructs a vector from a line
#include <vector>
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>
template <
class Value,
class Container = std::vector<Value>,
class charT = char,
@dgodfrey206
dgodfrey206 / prog1.cpp
Last active August 29, 2015 14:05
Project Euler #1
// Answer: 233168
int main()
{
int sum(0);
for (int i = 0; i < 1000; ++i) {
if ((i % 3 == 0) || (i % 5 == 0)) sum += i;
}
}
@dgodfrey206
dgodfrey206 / prog2.cpp
Last active August 29, 2015 14:05
Project Euler #2
// Answer: 4,613,732
unsigned int fib(unsigned int n) {
return n <= 1 ? n : fib(n-1) + fib(n-2);
}
int main()
{
unsigned int sum(0);