Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) tuple, std::tuple, boost::tuple

 

A tuple is an on-the-fly data type similar to std::pair, except for a bigger number of possible elements.

 

 

 

 

 

 

C++0xSTL std::tuple

 

 

#include <cassert>
#include <string>
#include <tuple>

int main()
{
  std::tuple<int,double,std::string> t
    = std::make_tuple(123,3.14159,"Bilderbikkel");


  const int x1 = std::get<0>(t);
  const double x2 = std::get<1>(t);
  const std::string x3 = std::get<2>(t);

  std::get<0>(t) = x1 + 1;
  std::get<1>(t) = x2 + 1.0;
  std::get<2>(t) = x3 + " was here";

  assert(std::get<0>(t) == 124);
  assert(std::get<1>(t) == 4.14159);
  assert(std::get<2>(t) == "Bilderbikkel was here");

}

 

 

 

 

 

C++98Boost boost::tuple

 

#include <iostream>
#include <string>
 
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
 
 
int main()
{
  boost::tuple<int,double,std::string> t = boost::make_tuple(123,3.14159,"Bilderbikkel");
 
  const int x1 = t.get<0>();
  const double x2 = t.get<1>();
  const std::string x3 = t.get<2>();
 
  boost::get<0>(t) = x1 + 1;
  boost::get<1>(t) = x2 + 1.0;
  boost::get<2>(t) = x3 + " was here";
 
  std::cout << t << std::endl;
}

 

Note the following:

  1. a boost::tuple is like an extended std::pair, with a maximum of ten elements
  2. similar to std::make_pair, for tuples you can use boost::make_tuple
  3. boost::get is used for getting and setting elements

 

 

 

 

 

External links

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict