A Simple C++ Triplet Template Class
Working with nested containers like pair<int, pair<int, int>> often leads to cluttered code with verbose access patterns such as .second.first. Alternatively, using tuple<int, int, int> introduces its own confusion with syntax like get<2>(x) where indices start from zero. A dedicated triplet template class provides a clean, intuitive solution that eliminates these pain points.
This implementation follows the same design philosophy as C++ standard library's pair, extending it with a third element while maintaining familiar usage patterns.
Obtaining the Code
The implementation is available on GitHub at https://github.com/Jerrycyx/triplet. Download the latest release from the releases page.
Integration Method 1: Header-Only
Place triplet.h in your project directory and include it with #include "triplet.h" (note the quotation marks required for local headers). Alterntaive locations are supported using relative or absolute paths.
Integration Method 2: Embedded in Single Files
For competitive programming contexts requiring a single submission file, you can copy the contents of triplet.min.h (excluding the header guard macros at the beginning and end) directly into your source code.
Usage Guide
The triplet template mirrors std::pair's interface, so users already familiar with standard containers will find the transition seamless.
Summary: The interface adds a .third member while construction, initialization, and comparison behave identical to pair.
Construction and Initialization
triplet<int, int, int> container; // Default construction
triplet<int, int, int> container(10, 20, 30); // Constructor initialization
triplet<int, int, int> container = {10, 20, 30}; // Brace initialization
Uninitialized triplets invoke the default constructor of their element types. For convenience, a factory function mirrors make_pair:
auto result = make_triplet(5, 15, 25); // Returns triplet<int, int, int>{5, 15, 25}
Accessing Elements
- First element:
container.first - Second element:
container.second - Third element:
container.third
The naming convention follows pair's established pattern, making it intuitive and memorable.
Comparison Operators
Six comparison operators are overloaded: ==, !=, <, >, <=, >=. The comparison semantics follow lexicographic ordering:
- Equality (
==): All three elements compare equal - Inequality (
!=): At least one element differs - Less than (
<): Comparesfirstfirst, thensecond, thenthirdas tiebreakers - Greater than (
>): Inverse of less then - Less than or equal (
<=): Equivalent to!(x > y) - Greater than or equal (
>=): Equivalent to!(x < y)
These operators require only that element types support < and == operations with a valid partial ordering (meaning x < y and y < x cannot both be true for any values).
License
This implementation is distributed under the Mozilla Public License 2.0 (MPL-2.0).
Key obligations include preserving original copyright notices, license headers, and disclaimers in all distributions. Modified versions distributed in source form must include appropriate notices and comply with MPL-2.0 terms. Original license declarations must not be removed or altered.