How to use PAIRS within a SET in C++

In this tutorial, we will learn briefly about pairs and sets in C++ and then how to use pairs within a set.

PAIRS IN C++

pair in C++ is a container that stores two values which can be of different data types.

SYNTAX :-  pair<datatype1, datatype2> pair_name;

To access the first element of the pair – pair_name. first and to access the second element – pair_name. second.

EXAMPLE :

A pair denoting the student name and marks obtained by him/her:

#include<bits/stdc++.h>
using namespace std;

int main(){
    pair < string , int > student ("Virat" , 97 );
    cout<< student . first <<" ";
    cout<< student . second <<" ";
    return 0;
}

OUTPUT :

Virat 97

SETS IN C++

Set can only have unique elements of a particular data type.

In a set, the value of the element identifies it and is always unique.

The elements of the set are always sorted.

SYNTAX-  set< datatype > set_name;

EXAMPLE:

#include <bits/stdc++.h>
using namespace std;

int main(){
    set<int> s;
    for(int i=10;i>=1;i--){
        s.insert(i);
    }
    s.insert(5);
    set<int>:: iterator it;
    for(it=s.begin();it!=s.end();it++){
        cout<< *it << " ";
    }
    return 0;
}

OUTPUT

1 2 3 4 5 6 7 8 9 10       The output is in ascending order

PAIRS WITHIN A SET

SYNTAX   set< pair < datatype1 , datatype2 >> set_name;

{ } is used to make a pair , alternatively make_pair( ) can also be used .

EXAMPLE:

Here we have a pair of student and marks obtained by the student.

Since here we have not specified the ordering which should be there in the set, the set will be sorted on the basis of the first element of the pair i.e names in ascending order.

If the first element of any two pairs is equal then the second element will be taken into consideration.

#include <bits/stdc++.h>
using namespace std;

int main(){
  set < pair < string , int > > s;
  
  //Insert student names in the set with marks obtained by them
  s.insert({"Ritu" , 85});
  s.insert({"Geeta" , 97});
  s.insert({"Aarti" , 56});
  s.insert({"Zara" , 96});
  s.insert({"Aarti" , 67});
  s.insert({"Zara" , 96});    //this pair already exists in the set
                              //this will only be printed 
  
  //Ordering will be on the basis of the names
  //DEFAULT COMPARISION 
  
  set < pair < string , int > > :: iterator it;
  for(it=s.begin();it!=s.end();it++){
      cout<< it->first << " " << it->second <<endl;
  }
  return 0;
}

OUTPUT:

Aarti 56
Aarti 67
Geeta 97
Ritu 85
Zara 96

Now we will order the set on the basis of the marks in descending order.

#include <bits/stdc++.h>
using namespace std;

struct comp{
    bool operator()(pair<string , int> a , pair<string , int> b){     
      	if(a.second!=b.second)
          return (a.second > b.second);
        return a.first < b.first;
    }
  //set will now be ordered on the basis of the second elements i.e. marks in descending order.
};

int main(){
  set < pair < string , int > ,comp >s;
  
  //Insert student names in the set with marks obtained by them
  s.insert({"Ritu" , 85});
  s.insert({"Geeta" , 97});
  s.insert({"Aarti" , 67});
  s.insert({"Zara" , 96});
  s.insert({"Aarti" , 56});
  s.insert({"Zara" , 96});    //this pair already exists in the set
                              //this will only be printed once
  //We have defined our own ordering here --comp 
  //set is ordered on the basis of that
  
  set < pair < string , int > > :: iterator it;
    for(it=s.begin();it!=s.end();it++){
        cout<< it->first << " " << it->second <<endl;
    }
    return 0;
}

OUTPUT

Geeta 97
Zara 96
Ritu 85
Aarti 67
Aarti 56

 

You may also read:

Leave a Reply