C++ tuple type

Introduction

While std::pair can hold only two values, the std::tuple wrapper can hold more than two values.

To use tuples, we need to include the <tuple> header.

To access a certain tuple element, we use the std::get<index_of_an_element>(tuple_name) function:

#include <iostream> 
#include <utility> 
#include <tuple> 

int main() //from  ww w . ja  va  2  s . c  o m
{ 
    std::tuple<char, int, double> mytuple = { 'a', 123, 3.14 }; 
    std::cout << "The first element is: " << std::get<0>(mytuple) << '\n'; 
    std::cout << "The second element is: " << std::get<1>(mytuple) << '\n'; 
    std::cout << "The third element is: " << std::get<2>(mytuple) << '\n'; 
} 



PreviousNext

Related