STLplus C++ Library Collection - containers/triple.hpp - A 3-element collection
Author: Andy Rushton

# STLplus C++ Library Collection

Website Collection Library

# containers/triple.hpp - A 3-element collection

- Browse Header triple.hpp
- Introduction
- Class Interface
- Utility Functions

## Introduction

The stlplus::triple class is similar to the std::pair class except it contains three objects, each of which can be of a different type. This class and its family of collection classes pair/triple/foursome are useful in themselves but are probably most useful in building data structures.

## Class Interface

The stlplus::triple class looks like this:

    template<typename T1, typename T2, typename T3>
    struct stlplus::triple
    {
      typedef T1 first_type;
      typedef T2 second_type;
      typedef T3 third_type;

      T1 first;
      T2 second;
      T3 third;

      triple(void);
      triple(const T1& p1, const T2& p2, const T3& p3);
      triple(const triple<T1,T2,T3>& t2);
    };

The class contains three elements called first, second and third. The constructor initialises the three elements, either from individual values or from another triple.

For example

    // construct a triple from elements
    stlplus::triple<unsigned,std::string,std::string> t1(0, "zero", "null");

    // construct a triple from another triple
    stlplus::triple<unsigned,std::string,std::string> t2(t1);

    // access the elements
    std::cout << t1.first << ":" << t1.second << ":" << t1.third << std::endl;

## Utility Functions

The header also provides utilities to help create triples and to compare them.

The stlplus::make_triple is analagous to the STL's std::make_pair:

    template<typename T1, typename T2, typename T3>
    triple<T1,T2,T3> stlplus::make_triple(const T1& first, const T2& second, const T3& third);

This can be used when assigning to an existing triple:

    stlplus::triple<unsigned,std::string,std::string> t1;
    t1 = stlplus::make_triple(0, "zero", "null");

The operator== template is provided to make it easy to compare two triples, provided they have the same element types:

    template<typename T1, typename T2, typename T3>
    bool operator == (const stlplus::triple<T1,T2,T3>& left, const stlplus::triple<T1,T2,T3>& right);

This can be used in any comparison:

    if (t1 == t2)
    {
      ...
    }