(adsbygoogle = window.adsbygoogle || []).push({});

Tuple in STL in C++

STD :: Tuple in STL in C++

1. Tuple is a template class in Standard Template Library (STL) in C++.

2. It is not a part of a container.

3. It is defined in <utility> header.

4. To use tuple, one should include tuple header file i.e, #include<tuple>

5. It is an object which can hold a number of elements.

6. The elements which are used in tuple can be of different data types.

7. The elements of tuples are initialized as arguments in the same order in which they are to be accessed.

8. There are different operations which can be used in tuples like get(), make_tuple(), swap(), etc. 

Just like in Pair in STL, we can pair two heterogeneous objects, in tuple we can pair multiple objects in Tuple at a time. We can insert value in tuple, access some values in tuple and can perform much more operations. 

Syntax of Tuple in C++

tuple <T1, T2, T2> Tuple_Name;

tuple <string, int, int> TUPLE1;

How to insert value in Tuple in C++

TUPLE1 = make_tuple("My Learning Mania", 101, 501);

How to access Elements of Tuple in C++

tuple <string, int, int> TUPLE1;
TUPLE1 = make_tuple ("My Learning Mania", 101, 501);

cout << std::get <0> (TUPLE1);
cout << std::get <1> (TUPLE1);
cout << std::get <2> (TUPLE1);

How to compare two Tuples in C++

You can compare two values of pairs by using the following syntax:

1.       ==      for equals
2.       >        for greater than
3.       !=       for not equals to
4.       <=      for less than or equals to
5.       <        for less than 
6.       >=      for greater than or equals to

using namespace std;
int main(){
  tuple <string, int, int>TUPLE1;
  TUPLE1 = make_tuple("My Learning Mania", 101, 501);
  cout << get <0> (TUPLE1) << "  ";
  cout << get <1> (TUPLE1) << "  ";
  cout << get <2> (TUPLE1) << "  ";

<< Previous                                                                                      Next >>
Next Post »