Skip to content

Instantly share code, notes, and snippets.

@duckie
duckie / cpp_file_simulatneous_read_write.cpp
Created June 27, 2012 12:51
C++ : How to write and read with only one file open for both operations
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <string>
std::ostream & format_2_l0(std::ostream & iStream)
{
return iStream << std::setw(2) << std::setfill('0');
}
@duckie
duckie / data_sort_by_member.cpp
Created June 27, 2012 13:19
C++ : How to sort a vector of a user struct/class by using one of its members
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct Donnees
{
int m_1;
std::string m_2;
float m_3;
@duckie
duckie / french_number_writer.cpp
Created August 2, 2012 18:28
Converts a number into its litteral french translation
#include <iostream>
#include <map>
#include <sstream>
/**
* Converts a number into its french representation
*
* @see http://fr.wikipedia.org/wiki/Adjectif_num%C3%A9ral
*/
class Convertisseur
@duckie
duckie / errors.h
Created August 3, 2012 10:24
Utility to parse a header, extract error codes and store them into a *.map re-usable from C++ code.
#ifndef IBM_ERRORS
#define IBM_ERRORS
/**
* C'est un peu la fête du slip les codes d'erreurs en macros
*/
#define ERREUR_MARCEL_A_TROP_BU 1
#define ERREUR_ROBERT_A_TROP_MANGE 2
# define ERREUR_Y_A_PLUS_DE_PASTIS 3
@duckie
duckie / initializer_list.cpp
Created January 15, 2013 11:01
C++11 initiliazer_list example
#include <iostream>
#include <initializer_list>
struct Point {
int m_a;
int m_b;
Point() :
m_a(0), m_b(0) {
}
@duckie
duckie / rank_by_field.cpp
Created February 20, 2013 13:23
C++11 : Compute a rank with a map.
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>
struct guy
{
std::string name_;
float mark_;
@duckie
duckie / gist:5012381
Last active December 14, 2015 02:18
C++11 trick to create some classes by their ID names.
#include <string>
#include <map>
#include <iostream>
#include <utility>
#include <functional>
struct Interface {
virtual void method() = 0; // May be a visitor pattern
};
@duckie
duckie / shrunken_dir.sh
Created April 9, 2013 00:58
A simple function to shrunken a Unix path to a descriptive fixed size string. I use this for my prompt.
function shrunken_dir(){
SEP="/"
ESC_SEP="\\$SEP"
HEADER_SIZE=5
SIZE_MAX=$1
current_disp=`pwd`
seen_path_max_length=$((SIZE_MAX-HEADER_SIZE))
current_seen_path=$(echo "$current_disp"|tr -s "$SEP")
nb_dirs=0
seen_path_length=$(expr length "$current_seen_path")
@duckie
duckie / timer.cpp
Created September 26, 2013 10:18
A C++11 example of a timer in a thread. This is one way in thousands to do it.
#include <iostream>
#include <chrono>
#include <thread>
#include <ctime>
#include <atomic>
void called() {
std::cout << "Coucou !" << std::endl;
}
@duckie
duckie / colorscale.cpp
Last active January 1, 2016 03:38
C++11 - Associate a RGB color to a given unsigned integral number in order to maximize local contrast. Two algorithms are given. The second is called in the main loop. With no arguments, a consistency check verifies that every possible color in the context are used, with no overlap. With 1 arguments, it outputs the colors from index 0 to ARG (in…
#include <array>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <tuple>
#include <set>
#include <map>
#include <vector>
namespace colorscale {