Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created December 25, 2014 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheSeamau5/7adca7a9496effee4357 to your computer and use it in GitHub Desktop.
Save TheSeamau5/7adca7a9496effee4357 to your computer and use it in GitHub Desktop.
Tuples in C++
#include <string>
using namespace std;
template <typename First, typename Second>
struct Tuple{
First first;
Second second;
};
template <typename First, typename Second>
Tuple<First, Second>* makeTuple(First first, Second second){
Tuple<First, Second>* tuple = (struct Tuple<First, Second>*) malloc (sizeof(struct Tuple<First, Second>));
tuple->first = first;
tuple->second = second;
return tuple;
}
// To avoid checks on generic types
string to_string(string s){ return s;}
template <typename First, typename Second>
string toString(Tuple<First,Second>* tuple){
if (tuple == NULL){
return "NULL";
}else{
return "(" + to_string(tuple->first) + ", " + to_string(tuple->second) + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment