C++ Standard Template Library (STL) – [Pair and Tuple]

Pair

Pair in C++ STL is used to combine two values having different or same data types. It provides a way to store two different values as a single element. The individual values of a pair can be accessed through its public members first and second.

To use STL built-in pair, you need to include <utility> header.

Code Implementation

//
//  main.cpp
//  Pair
//
//  Created by Himanshu on 11/04/22.
//

#include <iostream>
#include <utility>
#include <vector>
#include <string>
using namespace std;
 
int main () {
     
    //Initializations
    
    //using make_pair
    pair<int, int> pairRank = make_pair(1, 98);
    
    //using value init
    pair<int, string> pairNameFirst (1, "Anon");
    
    //using copy constructor
    pair<int, string> pairTemp(pairNameFirst);
    
    
    cout<<"pairRank values:"<<endl;
    cout<<pairRank.first<<", "<<pairRank.second<<endl;
    
    //accessing pair elements using method get<>
    cout<<"pairNameFirst values:"<<endl;
    cout<<get<0>(pairNameFirst)<<", "<<get<1>(pairNameFirst)<<endl<<endl;
    
    vector<pair<int, int>> pairRankVector;
    vector<pair<int, string>> pairNameVector;
    
    vector<pair<int, int>>::iterator rankItr;
    vector<pair<int, string>>::iterator nameItr;
    
    pairRankVector.push_back(pairRank);
    pairNameVector.push_back(pairNameFirst);
    
    vector<int> rankMarks = {95, 90, 87, 83, 75};
    vector<string> rankNames = {"Bruw", "Cync", "Droke", "Elph", "Frel"};
    
    
    int n = (int) rankMarks.size();
    
    for (int i=0; i<n; i++) {
        pairRankVector.push_back(make_pair(i+2, rankMarks[i]));
        pairNameVector.push_back(make_pair(i+2, rankNames[i]));
    }
   
    cout<<"Size of pairRankVector: "<<pairRankVector.size()<<endl<<endl;
    
    //accessing vector elements using iterator,
    //accessing pair elements using -> operator
    //accessing vector element using (*iterator)
    for (rankItr = pairRankVector.begin(), nameItr = pairNameVector.begin();
         rankItr != pairRankVector.end(); rankItr++, nameItr++) {
        
        cout<<rankItr->first<<" "<<nameItr->second<<" "<<(*rankItr).second<<endl;
    }
    
    return 0;
}

Output

pairRank values:
1, 98
pairNameFirst values:
1, Anon

Size of pairRankVector: 6

1 Anon 98
2 Bruw 95
3 Cync 90
4 Droke 87
5 Elph 83
6 Frel 75

Here’s a working example: Pair

Tuple

Tuple unlike pair can be used to combine two or more than two values having different or same data types.
To use STL built-in tuple, you need to include <tuple> header.

Code Implementation

//
//  main.cpp
//  Tuple
//
//  Created by Himanshu on 11/04/22.
//

#include <iostream>
#include <tuple>
#include <vector>
#include <string>
using namespace std;
 
int main () {
     
    //Initializations
    //using make_tuple
    tuple<int, string, int> tupleFirst = make_tuple(1, "Anon", 98);
    
    //using value init
    tuple<int, string, int> tupleLast (7, "Glee", 67);
    
    //using copy constructor
    tuple<int, string, int> tupleTemp(tupleLast);
    
    
    cout<<"tupleTemp values:"<<endl;
    cout<<get<0>(tupleTemp)<<", "<<get<1>(tupleTemp)<<", "<<get<2>(tupleTemp)<<endl<<endl;
    
    vector<int> rankMarks = {95, 90, 87, 83, 75};
    vector<string> rankNames = {"Bruw", "Cync", "Droke", "Elph", "Frel"};
    
    
    vector<tuple<int, string, int>> nameList;
    nameList.push_back(tupleFirst);
    
    int n = (int) rankMarks.size();
    
    for (int i=0; i<n; i++) {
        nameList.push_back(make_tuple(i+2, rankNames[i], rankMarks[i]));
    }
    
    nameList.push_back(tupleLast);
   
    cout<<"Size of (vector) nameList: "<<nameList.size()<<endl<<endl;
    
    for (tuple<int, string, int> t: nameList) {
        cout<<get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<endl;
    }
    
    return 0;
}

Output

tupleTemp values:
7, Glee, 67

Size of (vector) nameList: 7

1 Anon 98
2 Bruw 95
3 Cync 90
4 Droke 87
5 Elph 83
6 Frel 75
7 Glee 67

Here’s a working example: Tuple

Leave a Reply

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