Listing 6

#include <tuple>
#include <iomanip>
#include <iostream>
using std::tr1::tuple; using std::tr1::make_tuple; using std::tr1::get;
using std::cout; using std::boolalpha;

struct S
  { // simple struct that holds a value
  S(int i0) : i(i0) {}
  int i;
  };

bool operator<(const S& left, const S& right)
  { // compare two objects of type S for relative order
  cout << " comparing " << left.i << " to " << right.i
       << ", result is " << (left.i < right.i) << '\n';
  return left.i < right.i;
  }

int main()
  { // demonstrate operator< for tuple types
  cout << boolalpha;
  tuple<S, S, S> t0 = make_tuple(1, 2, 3);
  tuple<S, S, S> t1 = t0;
  cout << "t0 < t1:\n";
  bool res = t0 < t1;
  cout << " result is " << res << '\n';

  get<1>(t0) = 1;
  get<2>(t0) = 4;
  cout << "After first change to t0, ";
  cout << "t0 < t1:\n";
  res = t0 < t1;
  cout << " result is " << res << '\n';

  get<1>(t0) = 3;
  cout << "After second change to t0, ";
  cout << "t0 < t1:\n";
  res = t0 < t1;
  cout << " result is " << res << '\n';
  return 0;
  }