//
// Compile this with g++-4.3 --std=c++0x tuple.cpp -o tuple
//
#include <iostream>
#include <string>
#include <tuple>
typedef std::tuple<int, double, std::string> my_tuple_t;
static void dump(const my_tuple_t& a_tuple)
{
// get an element
std::cout << std::get<0>(a_tuple)
<< ", "
<< std::get<1>(a_tuple)
<< ", "
<< std::get<2>(a_tuple)
<< std::endl;
}
int main(int argc, char* argv[])
{
// creation
my_tuple_t a_tuple(1, 5.0);
dump(a_tuple);
// set an element
std::get<2>(a_tuple) = "a string";
// The following lines do not compile:
// std::get<0>(a_tuple) = "another string";
// std::cout << std::get<3>(a_tuple) << std::endl;
dump(a_tuple);
// helper make_tuple() function with type deduction
std:: cout << std::get<1>(std::make_tuple(false, std::string("see"))) << std::endl;
my_tuple_t another_tuple = std::make_tuple(2, 10.0, std::string("nothing"));
dump(another_tuple);
// test if two tuples are equal
std::cout << (a_tuple == another_tuple) << std::endl;
// assign a tuple to another
another_tuple = a_tuple;
dump(another_tuple);
// now they're equal
std::cout << (a_tuple == another_tuple) << std::endl;
// copy elements in variables, ignoring some
std::string a_string;
std::tie(std::ignore, std::ignore, a_string) = another_tuple;
std::cout << a_string << std::endl;
// concatenate two tuples into another
std::tuple<int, double, std::string, bool, std::string> cat =
std::tuple_cat(a_tuple, std::make_tuple(false, std::string("see")));
return 0;
}
syntax highlighted by Code2HTML, v. 0.9.1