Headers
Some Nice C++ Headers
All the includes
sometimes you just can't have enough includes from the standard library.
#include <vector> #include <iostream> #include <string> #include <cmath> #include <iomanip> #include <map> #include <random> #include <memory>
You can assume using namespace std;
in the following.
Sprinkle with std::
as appropriate if you don't like
to clutter up your namespace.
REM
The function that does the most important thing (as per the Tao De Ching): nothing! Takes any number of arguments and does .. nothing!
template <typename... Rest> void REM(Rest ...rest) { return; }
Example usage
// Here is a comment where the fact that I have 4 papers to complete gets buried with the rest of the comment // Here the information jumps out as code, just not evaluated. REM( "number of papers", 4 );
Dice
The goal here is to have the number of sides of the die decided at compile time. This probably doesn't increase efficiency significantly, but may reduce errors.
It is part of a general effort to push as much work to the comipler as possible.
template <int sides> class die { static std::uniform_int_distribution<int> my_uniform_dist; static std::default_random_engine my_random_engine; public: int operator() () { return my_uniform_dist(my_random_engine); } }; // end class die // in cpp file // ---------------- template <int sides> std::uniform_int_distribution<int> die<sides>::my_uniform_dist(1, sides); template <int sides> std::default_random_engine die<sides>::my_random_engine( std::random_device{}() ); // ---------------- // helper for the usual case inline int d6() { return die<6>{}(); } template <int n> inline int roll() { return d6() + roll<n-1>(); } template<> inline int roll<0> () { return 0; }
roll<n> is an exercise of template recursion, because template recursion is cool.
Usage
// roll one 6-sided die: cout << d6() << endl; // roll one 7-sided die: cout << die<7>{}() << endl; // roll 3 6-sided dice: cout << roll<3>() << endl;
Makefile
# Compiler CXX = g++ # Compiler flags CXXFLAGS = -g -Wall -fPIC # Target executable name TARGET = aws # Source files SOURCE = aws.cpp # Header files HEADERS = aws.h # Object files OBJECTS = $(SOURCE:.cpp=.o) # Default target all: $(TARGET) # Link the target executable $(TARGET): $(OBJECTS) $(CXX) -g $(OBJECTS) -o $(TARGET) $(CXX) -shared -o lib$(TARGET).so $(OBJECTS) echo cp $(TARGET) /data/data/com.termux/files/usr/bin/$(TARGET) # for use in termux echo chmod u+x /data/data/com.termux/files/usr/bin/$(TARGET) # for use in termux echo ./$(TARGET) # use this line if you want to run the target as part of the action # Compile source files to object files %.o: %.cpp $(HEADERS) echo $(CXXFLAGS) $(CXX) $(CXXFLAGS) -c $< -o $@ # Clean up clean: rm -f $(OBJECTS) $(TARGET)
There are some nonstandard lines here (just echoed by default)
cp $(TARGET) /data/data/com.termux/files/usr/bin/$(TARGET) chmod u+x /data/data/com.termux/files/usr/bin/$(TARGET)
If you are compiling on termux, it works fine but you can't run the application. So instead, copy the output to the path and run it.
function unzipme () { tar -xzvf $1.tar.gz } function cq() { calc -q "$*" } function tarthisdir () { tar -cvf $1.tar $1 }