Implement a Triplet class in C++
This post will discuss how to implement a Triplet class in C++.
1. Using std::tuple
Since C++11, we can use std::tuple to hold a collection of elements in C++. It is a generalization of std::pair and is available in the standard library in the header <tuple>. It can be used to store a triplet of the same type as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <tuple> int main() { std::tuple<int, int, int> triplet(1, 2, 3); std::cout << "(" << std::get<0>(triplet) << ", " << std::get<1>(triplet) << ", " << std::get<2>(triplet) << ")\n"; return 0; } |
Output:
(1, 2, 3)
The std::tuple allows each element to be of a different type. We can even create a type alias for triplets:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <tuple> template<typename T1, typename T2, typename T3> using Triplet = std::tuple<T1, T2, T3>; int main() { Triplet<int, double, std::string> triplet(1, 3.14, "PI"); std::cout << "(" << std::get<0>(triplet) << ", " << std::get<1>(triplet) << ", " << std::get<2>(triplet) << ")\n"; return 0; } |
Output:
(1, 2, 3)
2. Using custom class
Another option is to implement your Triplet class. Here’s an example of its usage using struct. The program implements a utility function to create a Triplet instance for the same element type:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> template<typename T> struct Triplet { T first, middle, last; }; template<typename T> Triplet<T> make_triplet(const T &x, const T &y, const T &z) { Triplet<T> t; t.first = x; t.middle = y; t.last = z; return t; } int main() { Triplet<int> t = make_triplet<int>(1, 2, 3); std::cout << "(" << t.first << ", " << t.middle << ", " << t.last << ")\n"; return 0; } |
Output:
(1, 2, 3)
Here’s an equivalent version using a class with the constructor.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> template<typename T> class Triplet { public: T first, middle, last; Triplet(const T &x, const T &y, const T &z) { this->first = x; this->middle = y; this->last = z; } }; int main() { Triplet<int> t(1, 2, 3); std::cout << "(" << t.first << ", " << t.middle << ", " << t.last << ")\n"; return 0; } |
Output:
(1, 2, 3)
We can easily extend the solution to have each element of a different type. The usage remains similar:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> template<typename T1, typename T2, typename T3> class Triplet { public: T1 first; T2 middle; T3 last; Triplet(const T1 &x, const T2 &y, const T3 &z) { this->first = x; this->middle = y; this->last = z; } }; int main() { Triplet<int, double, std::string> t(1, 3.14, "PI"); std::cout << "(" << t.first << ", " << t.middle << ", " << t.last << ")\n"; return 0; } |
Output:
(1, 2, 3)
3. Using Boost.Tuple
If your project uses the boost C++ library, you can use Boost.Tuple to construct a tuple, whose elements may be of the same or different types. Boost.Tuple is designed for ease of use and efficiency and can be used before C++11.
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <boost/tuple/tuple.hpp> int main() { boost::tuple<int, double, std::string> t(1, 3.14, "PI"); std::cout << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")\n"; return 0; } |
Output:
(1, 2, 3)
That’s all about implementing a Triplet class in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)