Sunday, April 27, 2014

C++ 11 tuple


In C++ 11 standard library, tuple is introduced which can hold values of heterogeneous data type in one object. It is also known as extended pair. We can keep any number of values in tuple.

Let’s see following example to understand how to use tuple,

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

using namespace std;

int main(int argc, char* argv[])
{
     tuple<string,int,double,int> info("Pranit",27,25000,86);

     return 0;
}




To use tuple we first need to include tuple (#include <tuple>) header file in C++ code. We will be using cout and string so we also included <string> and <iostream> header files. 

In code, we have initialized tuple info to keep information regarding ‘name’:string, ‘age’:int, ‘salary’:double, ‘year’:int.

tuple<string,int,double,int> info("Pranit",27,25000,86);

Here we have defined and initialized tuple at same place, we can also use convenience function to initialize value after defining it like,

tuple<string,int,double,int> info; //defining tuple
info = make_tuple("Pranit",27,25000,86); //initializing tuple

To access values of tuple we can use get function,
get<0>(info); // accessing name of info
get<2>(info); // accessing salary of info
get<3>(info); // accessing name of year

If by mistake you give out of range value to get function, it will give compile time error.

get<5>(info); //error :: index from 0 to 3 is only permissible

get function returns reference to element, so we can change value of element by assigning value.
get<2>(info) = 20000; // change value of  salary from 250000 to 20000

We can extract all values of tuple using std::tie function, also, if we want to ignore any of the value we can use std::ignore.

tie(name,age,salary,year) = info; // extracted all values in corresponding variables
tie(name,std::ignore,salary,year) = info; // ignoring age value

Value of one tuple can be assigned to another tuple using assignment operator.

tuple<string,int,double,int> info1;
info1 = info;

Complete code,

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

using namespace std;

int main(int argc, char* argv[])
{
     tuple<string,int,double,int> info("Pranit",27,25000,86);//defining and initilizing
     //tuple<string,int,double,int> info; //defining tuple
     //info = make_tuple("Pranit",27,25000,86); //initilizing tuple
    
     get<0>(info); // accessing name of info
     get<2>(info); // accessing salary of info
     get<3>(info); // accessing name of year

     //get<5>(info); //error :: index from 0 to 3 is only permissible

     get<2>(info) = 20000; // change value of  salary from 250000 to 20000

     string name;
     int age;
     double salary;
     int year;

     tie(name,age,salary,year) = info; // extracted all values in corresponding variables
     tie(name,std::ignore,salary,year) = info; // ignoring age value, other values are extracted

     tuple<string,int,double,int> info1;
     info1 = info;

     return 0;
}



No comments:

Post a Comment