Boost logo

Boost Users :

From: Chris Weed (chrisweed_at_[hidden])
Date: 2007-09-04 17:39:07


On 9/4/07, Graham Reitz <graham.cpp_at_[hidden]> wrote:
> One of my colleagues has asked me, "why not use a simple struct over a tuple?"
>
> Unfortunately, I couldn't give a convincing answer, which means I
> probably don't understand tuple well enough.
>
> What is a good way to respond to this? Then we can identify when it
> makes better sense to use a struct versus a tuple.
>

It probably depends on the application, but tuples are nice for
returning multiple values from a function. For example

boost::tuple<float,float,float>
min_mode_max(const std::vector<float>& v)
{
  float min,mode,max;
  ...
  return boost::make_tuple(min,mode,max);
}

std::vector<float> data = read_data('file.txt');
float min,mode,max;
boost::tie(min,mode,max) = min_mode_max(data);
std::cout << min << " " << mode << " " << max << std::endl;

There are many other ways you could do this, but I like the tuple version.
Chris


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net