What is a Tuple, a Pairs and a Tie in C++

Share the Article

What are Tuples?

In C++ language, the classes work as user defined data types. The class contains both data and function together. Therefore, to make the data more and more organized, C++11 has introduced a new concept of tuple. Tuples give very good flexibility for creating a collection of different or same data types.

In a class, we can access a data member or function using its name. However, in a tuple we use get<index> helper function for the same. Undoubtedly, in Object Oriented programming, the Tuple concept provides much more power for creating and handling multiple user defined data.

Header file required

#include<tuple>

How to define tuple data type

std::tuple<int,int> t1 (5,5); // t1 tuple holding 2 int values std::tuple<int,char,float>t2(5,'a',34.3); // t2 tuple holding int, char and float values std::tuple<char,string,float>t3('a',"MainFunda".23.80); // t3 tuple holding char, string and float

Basic Example of tuple in C++

Lets us create a Sample application for seeing how to set and get data from tuples

#include <iostream> //Main header file #include <string> // for string #include <tuple> //for tuple using namespace std; int main() { std::tuple<int, float, char,std::string> t1 (5,23.8,'a',"MainFunda"); //created tuple holding 4 data values int iData = std::get < 0 > (t1); // get 1st int value float fData= std::get < 1 > (t1); // get 2nd float value char cData = std::get<2>(t1); // get 3rd char vaue std::string strData = std::get < 3 > (t1); // get 3rd char value cout <<"Int is "<< iData <<"\nFloat is "<<fData <<"\nChar is "<<cData <<"\nString is "<<strData <<endl; return 0; }

As seen above getting data from a tuple object is very easy . We just need to call get<index-1> for knowing the content of data from tuple.

output of code using a tuple in c++

Error scenario with invalid get<index>

Lets see one more application where we are calling get<index-1> with invalid index

#include <iostream> //Main header file #include <string> // for string #include <tuple> //for tuple using namespace std; int main() { std::tuple<int, float, char,std::string> t1 (5,23.8,'a',"MainFunda"); //created tuple holding 4 data values int iData = std::get < 5 > (t1); // get 5th (invalid) value cout <<"Int is "<< iData <<endl; return 0; }

We are getting compile time error for accessing invalid index for tuple. This is the advantage of tuple .

compiler error on using wrong index in std:;get(index) with tuples

Tuple – Helper Function

get( )

The function get( ) takes an integer index as input and returns the reference to tuple data member at that index . In turn, we can use such reference for setting a tuple data member also. Below application demonstrates this

#include <iostream> //Main header file #include <string> // for string #include <tuple> //for tuple using namespace std; int main() { std::tuple<int, float, char,std::string> t1 (5,23.8,'a',"MainFunda"); //created tuple holding 4 data values int iData = std::get < 0 > (t1); // get first data member of tuple cout <<"Int is "<< iData <<endl; std::get<0>(t1) = 30; // set first data member of tuple iData = std::get<0>(t1); cout <<"Int after modification " << iData <<endl; return 0; }

Output

output of code demonstrating get( ) function with tuple

tuple_size( )

At any time we can get the size of tuple by using tuple_size( ) function.

#include <iostream> // main header #include <tuple> // tuple header #include<string> // string header using namespace std; int main () { std::tuple<int,char,float,std::string> t1 (1,'a',1.1,"MainFunda"); cout << "size of tuple is " <<std::tuple_size<decltype(t1)>::value << endl; // value contain the number of elements return 0; }

Output

output of code using tuple_size( ) function

make_tuple( )

The function make_tuple( ) is used for creating a tuple from a list of elements passed in the arguments. List of elements which we are passing as argument can be of different data types.

#include <iostream> //main header #include <tuple> // for tuple using namespace std; int main() { auto t1 = std::make_tuple(1,2.4, "MainFunda"); // tuple creation cout << "The value of t1 is " << std::get<0>(t1) << ", " << std::get<1>(t1) << ", " << std::get<2>(t1) <<endl; }

Output

output of code using make_tuple( ) function

forward_as_tuple( )

The function forward_as_tuple( ) creates a temporary tuple as r-value reference from argument passed as parameter to this function.

// forward_as_tuple example #include <iostream> // main header #include <tuple> // for tuple using namespace std; void show_Data(std::tuple<int&&, int&&> t1) { std::cout << std::get<0>(t1) << ", " << std::get<1>(t1) << endl; } int main() { show_Data (std::forward_as_tuple(30,30)); return 0; }

Output

output of code using forward_as_tuple( ) function for forwarding a tuple in c++

tuple_cat( )

The function tuple_cat( ) concatenates two or more tuples which are passed as the arguments.

#include <iostream> //main header #include <string> // for string #include <tuple> // for tuple using namespace std; int main () { std::tuple<char,std::string> t1 ('c',"MainFunda"); std::pair<int,float> t2 (100,12.5); auto t3 = std::tuple_cat ( t1, t2 ); std::cout << "ConCat T3" <<endl ; std::cout << std::get<0>(t3) << endl; std::cout << std::get<1>(t3) << endl; std::cout << std::get<2>(t3) << endl; std::cout << std::get<3>(t3) << endl; auto t4 = std::tuple_cat(t3, std::make_tuple(" test", "begin")); std::cout << "ConCat T4" <<endl ; std::cout << std::get<0>(t4) << endl; std::cout << std::get<1>(t4) << endl; std::cout << std::get<2>(t4) << endl; std::cout << std::get<3>(t4) << endl; std::cout << std::get<4>(t4) << endl; std::cout << std::get<5>(t4) << endl; return 0; }

Output

output of code using tuple_cat( ) function

What are Pairs in C++?

Pairs creates class template from 2 different or same datatypes . It is a specialized version of tuple with 2 elements.

Header File required for pairs

#include <utility>

Basic example of Pairs

lets see below simple example for creating and displaying the Pair content

#include <iostream> // main header #include <utility> // for pair using namespace std; int main () { std::pair <int,float> p1 (1,2); std::cout << "foo: " << p1.first << ", " << p1.second << endl; return 0; }

Output

basic example of using std::pairs< >

Pair-Helper Functions

make_pair( )

This function creates a pair object from its first and second argument.

#include <iostream> // main header #include <utility> // for pair using namespace std; int main () { std::pair <int,float> p1 = make_pair(1,2); std::cout << "foo: " << p1.first << ", " << p1.second << endl; return 0; }

Output

output of basic example with make_pair( ) function

What is a Tie of in C++?

We have read tuple data types above. A tie creates an L-vlaue tuple for future use. Tie is mainly used for extracting element from tuple.

Basic Example with Tie

// packing/unpacking tuples #include <iostream> // std::cout #include <tuple> // std::tuple, std::make_tuple, std::tie int main () { int i1; float f1; std::tuple<int,float> t1 = std::make_tuple (1, 22.3); // packing values into tuple std::tie (i1,f1) = t1; //unpacking tuple into variables std::cout << "1st element " << i1 << '\n'; std::cout << "2nd element: " << f1 << '\n'; //example to ignore some element std::tie (i1, std::ignore) = t1; // unpacking tuple into variables //and ignoring 2ns element std::cout << "1st element " << i1 << '\n'; return 0; }

Output

output of code using std::tie< >

Main Funda: Tuples provides additional flexibility to play around with user-defined data types

Related Topics:

Lambda in C++11
Lambda in C++17
What are the drawbacks of using enum ?
Which member functions are generated by compiler in class?
C++ Multithreading: Understanding Threads
How to stop compiler from generating special member functions?
Compiler Generated Destructor is always non-virtual
How to make a class object un-copyable?
Why virtual functions should not be called in constructor & destructor ?
Explaining C++ casts
How std::forward( ) works?
Rule of Three
How std::move() function works?
What is reference collapsing?
How delete keyword can be used to filter polymorphism
emplace_back vs push_back

Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *