Tuesday, August 22, 2017

C++11: tuple

You can now make ordered lists of mixed types called tuples. Here is an example:

#include <iostream>
#include <string>
#include <tuple>

int main()
{
  std::tuple<char, int, double, const char *, std::string> s;
  std::tuple<char, int, double, const char *, std::string> t;

  s = std::make_tuple('A', 1, 2.5, "three", "four");
  t = std::make_tuple('A', 1, 2.5, "three", "four");

  if (s == t) std::cout << "Tuples Match, ";

  double myDouble = std::get<2>(s);
  std::cout << myDouble << std::endl;

  return 0;
}
// Output: Tuples Match, 2.5
Reference: https://isocpp.org/wiki/faq/cpp11-library#tuple
                  http://en.cppreference.com/w/cpp/utility/tuple

No comments:

Post a Comment