Created
December 25, 2014 02:58
-
-
Save TheSeamau5/7adca7a9496effee4357 to your computer and use it in GitHub Desktop.
Tuples in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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