TUPLE in STL
pair are very similar in their structure. Just like in pair we can pair two heterogeneous object, in tuple we can pair three heterogeneous objects.
SYNTAX of a tuple is:
// creates tuple of three object of type T1, T2 and T3
tuple<T1, T2, T3> tuple1;
Tuple Template: Some Commonly used Functions
Similar to pair, tuple template has its own member and non-member functions, few of which are listed below :
- A Constructor to construct a new tuple
= : to assign value to a tuple
- swap : to swap value of two tuples
- make_tuple() : creates and return a tuple having elements described by the parameter list.
- Operators( == , != , > , < , <= , >= ) : lexicographically compares two pairs.
- Tuple_element : returns the type of tuple element
- Tie : Tie values of a tuple to its refrences.
Program demonstrating Tuple template
int main ()
tuple<int, int, int> tuple1; //creates tuple of integers
tuple<int, string, string> tuple2; // creates pair of an integer an 2 string
tuple1 = make_tuple(1,2,3); // insert 1, 2 and 3 to the tuple1
tuple2 = make_pair(1,"Studytonight", "Loves You");
/* insert 1, "Studytonight" and "Loves You" in tuple2 */
string first_name, last_name;
tie(id,first_name,last_name) = tuple2;
/* ties id, first_name, last_name to
first, second and third element of tuple2 */
cout << id <<" "<< first_name <<" "<< last_name;
/* prints 1 Studytonight Loves You */