STL-pair-like triplet class - do I roll my own?
Solution 1
No, don't roll your own. Instead, take a look at std::tuple
- it's a generalization of std::pair
. So here's a value-initialized triple of int
s:
std::tuple<int, int, int> triple;
If you want, you can have a type alias for triples only:
template<typename T1, typename T2, typename T3>
using triple = std::tuple<T1, T2, T3>;
Solution 2
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.
Solution 3
I think you need something like this:
template<typename T>
struct triplet
{
T first, middle, last;
};
template<typename T>
triplet<T> make_triplet(const T &m1, const T &m2, const T &m3)
{
triplet<T> ans;
ans.first = m1;
ans.middle = m2;
ans.last = m3;
return ans;
}
Examples of usage:
triplet<double> aaa;
aaa = make_triplet<double>(1.,2.,3.);
cout << aaa.first << " " << aaa.middle << " " << aaa.last << endl;
triplet<bool> bbb = make_triplet<bool>(false,true,false);
cout << bbb.first << " " << bbb.middle << " " << bbb.last << endl;
I'm using this and it is enough for my purposes... If you want different types, though, just do some modifications:
template<typename T1, typename T2, typename T3>
struct triplet
{
T1 first;
T2 middle;
T3 last;
};
template<typename T1, typename T2, typename T3>
triplet<T1,T2,T3> make_triplet(const T1 &m1, const T2 &m2, const T3 &m3)
{
triplet<T1,T2,T3> ans;
ans.first = m1;
ans.middle = m2;
ans.last = m3;
return ans;
}
And the usage will be very similar:
triplet<bool,string,myDouble> ccc;
ccc = make_triplet<bool,string,double>(false,"AB",3.1415);
ccc.middle = "PI";
cout << ccc.first << " " << ccc.middle << " " << ccc.last << endl;
einpoklum
Made my way from the Olympus of Complexity Theory, Probabilistic Combinatorics and Property Testing to the down-to-earth domain of Heterogeneous and GPU Computing, and now I'm hoping to bring the gospel of GPU and massive-regularized parallelism to DBMS architectures. I've post-doc'ed at the DB architecture group in CWI Amsterdam to do (some of) that. I subscribe to most of Michael Richter's critique of StackOverflow; you might want to take the time to read it. If you listen closely you can hear me muttering "Why am I not socratic again already?"
Updated on April 17, 2021Comments
-
einpoklum about 3 years
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?
-
einpoklum about 7 yearsOh no, a triplet class should definitely not be a subclass of
std::pair
. A triplet is not a kind-of-a-pair, like astd::pair<T,U>
is not a subclass ofT
nor ofU
. -
Bert Bril about 7 yearsI don't see your point. A twin is a pair where Tand U are the same class, triplet has 3 of the same type. Any triplet can be usef where any twin can be usef.
-
einpoklum about 7 yearsThat is absolutely not true. With a pair, you rely on there being exactly two pieces of data; with a triplet, you make a contradictory assumption. Not only can they not be subclasses, but they cannot be used interchangeably.
-
Bert Bril about 7 yearsSorry I disagree. Anywhere you need a twin you can use a triplet, but not the other way around. That's exactly the idea of inheritance, the LSK applies perfectly. Moreover, because the members are called 'first' and 'second' parameterized functions and classes get exactly what they need from a pair even when in fact they operate on a triplet. Very standard and very OK.
-
user1956185 over 6 yearsThere is a small mistake in your code. The definition of
make_triplet
should betriplet<T1,T2,T3> make_triplet(const T1 &m1, const T2 &m2, const T3 &m3)
-
Neo li almost 3 yearsGet error message when I use:
vector< triplet<double, double, double> > v; v.emplace_back(1,2,3);