I want to use a triplet class, as similar as possible to std::pair. STL doesn't seem to have one. I don't want to use something too heavy, like Boost. Is there some useful FOSS non-restrictive-license triplet class I could lift from somewhere? Should I roll my own? Should I do something else entirely?
Edit: About std::tuple
...
Is there really no benefit to a triplet-specific class? I mean, with tuple, I can't do
template<typename T1, typename T2, typename T3> std::tuple<T1, T2, T3> triple;
now can I? Won't I have to typedef individual-type-combination triples?
To add the aspect of similarity to std::pair, you could define a class that has first, second and third. My guess is that in In that case you cannot avoid making a subclass of std::pair and add a 'third' member. It's a few lines of code.
Furthermore, if you often use these things with the same type, you could make twins and triplet classes as well. Easily made with C++-11's 'using', like:
No, don't roll your own. Instead, take a look at
std::tuple
- it's a generalization ofstd::pair
. So here's a value-initialized triple ofint
s:If you want, you can have a type alias for triples only:
If you can use C++11, use std::tuple
If not, use boost::tuple
If you're using C++11, you can use
std::tuple
.If you're using C++03, then you'll either need to resort to rolling your own (which isn't too hard), or using
tuple
from Boost.I think you need something like this:
Examples of usage:
I'm using this and it is enough for my purposes... If you want different types, though, just do some modifications:
And the usage will be very similar: