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

# STLplus C++ Library Collection

Website Collection Library

# containers/foursome.hpp - A 4-element collection

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

## Introduction

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

Note:This was originally called quadruple, but it turned out that both this and quad conflicted with some system headers on Solaris. So it eventually got renamed foursome.

## Class Interface

The stlplus::foursome class looks like this:

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

      T1 first;
      T2 second;
      T3 third;
      T4 fourth;

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

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

For example

    // construct a foursome from elements
    stlplus::foursome<unsigned,std::string,std::string,unsigned> f1(0, "zero", "help", 28);

    // construct a foursome from another foursome
    stlplus::foursome<unsigned,std::string,std::string> f2(f1);

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

## Utility Functions

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

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

    template<typename T1, typename T2, typename T3, typename T4>
    foursome<T1,T2,T3,T4> make_foursome(const T1& first, const T2& second, const T3& third, const T4& fourth);

This can be used when assigning to an existing foursome:

    stlplus::foursome<unsigned,std::string,std::string,unsigned> f1;
    f1 = stlplus::make_foursome(0, "zero", "null", 28);

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

    template<typename T1, typename T2, typename T3, typename T4>
    bool operator == (const foursome<T1,T2,T3,T4>& left, const foursome<T1,T2,T3,T4>& right);

This can be used in any comparison:

    if (f1 == f2)
    {
      ...
    }